index.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. Introduction
  2. ============
  3. Doctrine Cache is a library that provides an interface for caching data.
  4. It comes with implementations for some of the most popular caching data
  5. stores. Here is what the ``Cache`` interface looks like.
  6. .. code-block:: php
  7. namespace Doctrine\Common\Cache;
  8. interface Cache
  9. {
  10. public function fetch($id);
  11. public function contains($id);
  12. public function save($id, $data, $lifeTime = 0);
  13. public function delete($id);
  14. public function getStats();
  15. }
  16. Here is an example that uses Memcache.
  17. .. code-block:: php
  18. use Doctrine\Common\Cache\MemcacheCache;
  19. $memcache = new Memcache();
  20. $cache = new MemcacheCache();
  21. $cache->setMemcache($memcache);
  22. $cache->set('key', 'value');
  23. echo $cache->get('key') // prints "value"
  24. Drivers
  25. =======
  26. Doctrine ships with several common drivers that you can easily use.
  27. Below you can find information about all the available drivers.
  28. ApcCache
  29. --------
  30. The ``ApcCache`` driver uses the ``apc_fetch``, ``apc_exists``, etc. functions that come
  31. with PHP so no additional setup is required in order to use it.
  32. .. code-block:: php
  33. $cache = new ApcCache();
  34. ApcuCache
  35. ---------
  36. The ``ApcuCache`` driver uses the ``apcu_fetch``, ``apcu_exists``, etc. functions that come
  37. with PHP so no additional setup is required in order to use it.
  38. .. code-block:: php
  39. $cache = new ApcuCache();
  40. ArrayCache
  41. ----------
  42. The ``ArrayCache`` driver stores the cache data in PHPs memory and is not persisted anywhere.
  43. This can be useful for caching things in memory for a single process when you don't need
  44. the cache to be persistent across processes.
  45. .. code-block:: php
  46. $cache = new ArrayCache();
  47. ChainCache
  48. ----------
  49. The ``ChainCache`` driver lets you chain multiple other drivers together easily.
  50. .. code-block:: php
  51. $arrayCache = new ArrayCache();
  52. $apcuCache = new ApcuCache();
  53. $cache = new ChainCache([$arrayCache, $apcuCache]);
  54. CouchbaseBucketCache
  55. --------------------
  56. The ``CouchbaseBucketCache`` driver uses Couchbase to store the cache data.
  57. .. code-block:: php
  58. $bucketName = 'bucket-name';
  59. $authenticator = new Couchbase\PasswordAuthenticator();
  60. $authenticator->username('username')->password('password');
  61. $cluster = new CouchbaseCluster('couchbase://127.0.0.1');
  62. $cluster->authenticate($authenticator);
  63. $bucket = $cluster->openBucket($bucketName);
  64. $cache = new CouchbaseBucketCache($bucket);
  65. FilesystemCache
  66. ---------------
  67. The ``FilesystemCache`` driver stores the cache data on the local filesystem.
  68. .. code-block:: php
  69. $cache = new FilesystemCache('/path/to/cache/directory');
  70. MemecacheCache
  71. --------------
  72. The ``MemcacheCache`` drivers stores the cache data in Memcache.
  73. .. code-block:: php
  74. $memcache = new Memcache();
  75. $memcache->connect('localhost', 11211);
  76. $cache = new MemcacheCache();
  77. $cache->setMemcache($memcache);
  78. MemcachedCache
  79. --------------
  80. The ``MemcachedCache`` drivers stores the cache data in Memcached.
  81. .. code-block:: php
  82. $memcached = new Memcached();
  83. $cache = new MemcachedCache();
  84. $cache->setMemcached($memcached);
  85. MongoDBCache
  86. ------------
  87. The ``MongoDBCache`` drivers stores the cache data in a MongoDB collection.
  88. .. code-block:: php
  89. $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
  90. $collection = new MongoDB\Collection($manager, 'database_name', 'collection_name');
  91. $cache = new MongoDBCache($collection);
  92. PhpFileCache
  93. ------------
  94. The ``PhpFileCache`` driver stores the cache data on the local filesystem like the
  95. ``FilesystemCache`` driver except the data is serialized using the ``serialize()``
  96. and ``unserialize()`` functions available in PHP. The files are included so this means
  97. that the data can be cached in PHPs opcache.
  98. .. code-block:: php
  99. $cache = new PhpFileCache('/path/to/cache/directory');
  100. PredisCache
  101. -----------
  102. The ``PredisCache`` driver stores the cache data in Redis
  103. and depends on the ``predis/predis`` package which can be installed with composer.
  104. .. code-block:: bash
  105. $ composer require predis/predis
  106. Then you can use the ``Predis\Client`` class to pass to the ``PredisCache`` class.
  107. .. code-block:: php
  108. $client = new Predis\Client();
  109. $cache = new PredisCache($client);
  110. RedisCache
  111. ----------
  112. The ``RedisCache`` driver stores the cache data in Redis and depends on the
  113. ``phpredis`` extension which can be found `here <https://github.com/phpredis/phpredis>`_.
  114. .. code-block:: php
  115. $redis = new Redis();
  116. $cache = new RedisCache($redis);
  117. RiakCache
  118. ---------
  119. The ``RiakCache`` driver stores the cache data in Riak and depends on the
  120. ``riak`` extension which can be found `here <https://github.com/php-riak/php_riak>`_.
  121. .. code-block:: php
  122. $connection = new Riak\Connection('localhost', 8087);
  123. $bucket = new Riak\Bucket($connection, 'bucket_name');
  124. $cache = new RiakCache($bucket);
  125. SQLite3Cache
  126. ------------
  127. The ``SQLite3Cache`` driver stores the cache data in a SQLite database and depends on the
  128. ``sqlite3`` extension which can be found `here <http://php.net/manual/en/book.sqlite3.php>`_.
  129. .. code-block:: php
  130. $db = new SQLite3('mydatabase.db');
  131. $cache = new SQLite3Cache($db, 'table_name');
  132. VoidCache
  133. ---------
  134. The ``VoidCache`` driver does not store the cache data anywhere. This can
  135. be useful for test environments where you don't want to cache the data
  136. anywhere but need to satisfy the dependency for the ``Doctrine\Common\Cache\Cache``
  137. interface.
  138. .. code-block:: php
  139. $cache = new VoidCache();
  140. WinCacheCache
  141. -------------
  142. The ``WinCacheCache`` driver uses the ``wincache_ucache_get``, ``wincache_ucache_exists``, etc. functions that come
  143. with the ``wincache`` extension which can be found `here <http://php.net/manual/en/book.wincache.php>`_.
  144. .. code-block:: php
  145. $cache = new WinCacheCache();
  146. XcacheCache
  147. -----------
  148. The ``XcacheCache`` driver uses functions that come with the ``xcache``
  149. extension which can be found `here <https://xcache.lighttpd.net/>`_.
  150. .. code-block:: php
  151. $cache = new XcacheCache();
  152. ZendDataCache
  153. -------------
  154. The ``ZendDataCache`` driver uses the Zend Data Cache API available in the Zend Platform.
  155. .. code-block:: php
  156. $cache = new ZendDataCache();
  157. Custom Drivers
  158. ==============
  159. If you want to implement your own cache driver, you just need to implement
  160. the ``Doctrine\Common\Cache\Cache`` interface. Here is an example implementation
  161. skeleton.
  162. .. code-block:: php
  163. use Doctrine\Common\Cache\Cache;
  164. class MyCacheDriver implements Cache
  165. {
  166. public function fetch($id)
  167. {
  168. // fetch $id from the cache
  169. }
  170. public function contains($id)
  171. {
  172. // check if $id exists in the cache
  173. }
  174. public function save($id, $data, $lifeTime = 0)
  175. {
  176. // save $data under $id in the cache for $lifetime
  177. }
  178. public function delete($id)
  179. {
  180. // delete $id from the cache
  181. }
  182. public function getStats()
  183. {
  184. // get cache stats
  185. }
  186. }