Something went wrong. Try again.
@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
Something went wrong. Try again.
2.8 kB · 118 lines
PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119<?php
/** * Key-value cache implemented in the current request. All storage is local * to this request (i.e., the current page) and destroyed after the request * exits. This means the first request to this cache for a given key on a page * will ALWAYS miss. * * This cache exists mostly to support unit tests. In a well-designed * applications, you generally should not be fetching the same data over and * over again in one request, so this cache should be of limited utility. * If using this cache improves application performance, especially if it * improves it significantly, it may indicate an architectural problem in your * application. */final class PhutilInRequestKeyValueCache extends PhutilKeyValueCache {
private $cache = array(); private $ttl = array(); private $limit = 0;
/** * Set a limit on the number of keys this cache may contain. * * When too many keys are inserted, the oldest keys are removed from the * cache. Setting a limit of `0` disables the cache. * * @param int $limit Maximum number of items to store in the cache. * @return $this */ public function setLimit($limit) { $this->limit = $limit; return $this; }
/* -( Key-Value Cache Implementation )------------------------------------- */
public function isAvailable() { return true; }
public function getKeys(array $keys) { $results = array(); $now = time(); foreach ($keys as $key) { if (!isset($this->cache[$key]) && !array_key_exists($key, $this->cache)) { continue; } if (isset($this->ttl[$key]) && ($this->ttl[$key] < $now)) { continue; } $results[$key] = $this->cache[$key]; }
return $results; }
public function setKeys(array $keys, $ttl = null) {
foreach ($keys as $key => $value) { $this->cache[$key] = $value; }
if ($ttl) { $end = time() + $ttl; foreach ($keys as $key => $value) { $this->ttl[$key] = $end; } } else { foreach ($keys as $key => $value) { unset($this->ttl[$key]); } }
if ($this->limit) { $count = count($this->cache); if ($count > $this->limit) { $remove = array(); foreach ($this->cache as $key => $value) { $remove[] = $key;
$count--; if ($count <= $this->limit) { break; } }
$this->deleteKeys($remove); } }
return $this; }
public function deleteKeys(array $keys) { foreach ($keys as $key) { unset($this->cache[$key]); unset($this->ttl[$key]); }
return $this; }
public function getAllKeys() { return $this->cache; }
public function destroyCache() { $this->cache = array(); $this->ttl = array();
return $this; }
}