@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
at upstream/main 121 lines 3.3 kB view raw
1<?php 2 3/** 4 * Interface to a key-value cache like Memcache or APCu. This class provides a 5 * uniform interface to multiple different key-value caches and integration 6 * with PhutilServiceProfiler. 7 * 8 * @task kvimpl Key-Value Cache Implementation 9 */ 10abstract class PhutilKeyValueCache extends Phobject { 11 12 13/* -( Key-Value Cache Implementation )------------------------------------- */ 14 15 16 /** 17 * Determine if the cache is available. For example, the APCu cache tests if 18 * APCu is installed. If this method returns false, the cache is not 19 * operational and can not be used. 20 * 21 * @return bool True if the cache can be used. 22 * @task kvimpl 23 */ 24 public function isAvailable() { 25 return false; 26 } 27 28 29 /** 30 * Get a single key from cache. See @{method:getKeys} to get multiple keys at 31 * once. 32 * 33 * @param string $key Key to retrieve. 34 * @param mixed $default (optional) Value to return if the key is not 35 * found. By default, returns null. 36 * @return mixed Cache value (on cache hit) or default value (on cache 37 * miss). 38 * @task kvimpl 39 */ 40 final public function getKey($key, $default = null) { 41 $map = $this->getKeys(array($key)); 42 return idx($map, $key, $default); 43 } 44 45 46 /** 47 * Set a single key in cache. See @{method:setKeys} to set multiple keys at 48 * once. 49 * 50 * See @{method:setKeys} for a description of TTLs. 51 * 52 * @param string $key Key to set. 53 * @param mixed $value Value to set. 54 * @param int|null $ttl (optional) TTL. 55 * @return $this 56 * @task kvimpl 57 */ 58 final public function setKey($key, $value, $ttl = null) { 59 return $this->setKeys(array($key => $value), $ttl); 60 } 61 62 63 /** 64 * Delete a key from the cache. See @{method:deleteKeys} to delete multiple 65 * keys at once. 66 * 67 * @param string $key Key to delete. 68 * @return $this 69 * @task kvimpl 70 */ 71 final public function deleteKey($key) { 72 return $this->deleteKeys(array($key)); 73 } 74 75 76 /** 77 * Get data from the cache. 78 * 79 * @param list<string> $keys List of cache keys to retrieve. 80 * @return array<string, mixed> Dictionary of keys that were found in the 81 * cache. Keys not present in the cache are 82 * omitted, so you can detect a cache miss. 83 * @task kvimpl 84 */ 85 abstract public function getKeys(array $keys); 86 87 88 /** 89 * Put data into the key-value cache. 90 * 91 * With a TTL ("time to live"), the cache will automatically delete the key 92 * after a specified number of seconds. By default, there is no expiration 93 * policy and data will persist in cache indefinitely. 94 * 95 * @param array<string, mixed> $keys Map of cache keys to values. 96 * @param int|null $ttl (optional) TTL for cache keys, in seconds. 97 * @return $this 98 * @task kvimpl 99 */ 100 abstract public function setKeys(array $keys, $ttl = null); 101 102 103 /** 104 * Delete a list of keys from the cache. 105 * 106 * @param list<string> $keys List of keys to delete. 107 * @return $this 108 * @task kvimpl 109 */ 110 abstract public function deleteKeys(array $keys); 111 112 113 /** 114 * Completely destroy all data in the cache. 115 * 116 * @return $this 117 * @task kvimpl 118 */ 119 abstract public function destroyCache(); 120 121}