]> git.mxchange.org Git - friendica.git/blob - library/Smarty/demo/plugins/cacheresource.memcache.php
reverting tinymce changes, updating smarty to 3.1.19
[friendica.git] / library / Smarty / demo / plugins / cacheresource.memcache.php
1 <?php
2
3 /**
4  * Memcache CacheResource
5  * CacheResource Implementation based on the KeyValueStore API to use
6  * memcache as the storage resource for Smarty's output caching.
7  * Note that memcache has a limitation of 256 characters per cache-key.
8  * To avoid complications all cache-keys are translated to a sha1 hash.
9  *
10  * @package CacheResource-examples
11  * @author  Rodney Rehm
12  */
13 class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore
14 {
15     /**
16      * memcache instance
17      *
18      * @var Memcache
19      */
20     protected $memcache = null;
21
22     public function __construct()
23     {
24         $this->memcache = new Memcache();
25         $this->memcache->addServer('127.0.0.1', 11211);
26     }
27
28     /**
29      * Read values for a set of keys from cache
30      *
31      * @param  array $keys list of keys to fetch
32      *
33      * @return array   list of values with the given keys used as indexes
34      * @return boolean true on success, false on failure
35      */
36     protected function read(array $keys)
37     {
38         $_keys = $lookup = array();
39         foreach ($keys as $k) {
40             $_k = sha1($k);
41             $_keys[] = $_k;
42             $lookup[$_k] = $k;
43         }
44         $_res = array();
45         $res = $this->memcache->get($_keys);
46         foreach ($res as $k => $v) {
47             $_res[$lookup[$k]] = $v;
48         }
49
50         return $_res;
51     }
52
53     /**
54      * Save values for a set of keys to cache
55      *
56      * @param  array $keys   list of values to save
57      * @param  int   $expire expiration time
58      *
59      * @return boolean true on success, false on failure
60      */
61     protected function write(array $keys, $expire = null)
62     {
63         foreach ($keys as $k => $v) {
64             $k = sha1($k);
65             $this->memcache->set($k, $v, 0, $expire);
66         }
67
68         return true;
69     }
70
71     /**
72      * Remove values from cache
73      *
74      * @param  array $keys list of keys to delete
75      *
76      * @return boolean true on success, false on failure
77      */
78     protected function delete(array $keys)
79     {
80         foreach ($keys as $k) {
81             $k = sha1($k);
82             $this->memcache->delete($k);
83         }
84
85         return true;
86     }
87
88     /**
89      * Remove *all* values from cache
90      *
91      * @return boolean true on success, false on failure
92      */
93     protected function purge()
94     {
95         $this->memcache->flush();
96     }
97 }