]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/demo/plugins/cacheresource.memcache.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / 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         if (class_exists('Memcached')) {
25             $this->memcache = new Memcached();
26         } else {
27             $this->memcache = new Memcache();
28         }
29         $this->memcache->addServer('127.0.0.1', 11211);
30     }
31
32     /**
33      * Read values for a set of keys from cache
34      *
35      * @param  array $keys list of keys to fetch
36      *
37      * @return array   list of values with the given keys used as indexes
38      * @return boolean true on success, false on failure
39      */
40     protected function read(array $keys)
41     {
42         $_keys = $lookup = array();
43         foreach ($keys as $k) {
44             $_k = sha1($k);
45             $_keys[] = $_k;
46             $lookup[ $_k ] = $k;
47         }
48         $_res = array();
49         $res = $this->memcache->get($_keys);
50         foreach ($res as $k => $v) {
51             $_res[ $lookup[ $k ] ] = $v;
52         }
53
54         return $_res;
55     }
56
57     /**
58      * Save values for a set of keys to cache
59      *
60      * @param  array $keys   list of values to save
61      * @param  int   $expire expiration time
62      *
63      * @return boolean true on success, false on failure
64      */
65     protected function write(array $keys, $expire = null)
66     {
67         foreach ($keys as $k => $v) {
68             $k = sha1($k);
69             $this->memcache->set($k, $v, 0, $expire);
70         }
71
72         return true;
73     }
74
75     /**
76      * Remove values from cache
77      *
78      * @param  array $keys list of keys to delete
79      *
80      * @return boolean true on success, false on failure
81      */
82     protected function delete(array $keys)
83     {
84         foreach ($keys as $k) {
85             $k = sha1($k);
86             $this->memcache->delete($k);
87         }
88
89         return true;
90     }
91
92     /**
93      * Remove *all* values from cache
94      *
95      * @return boolean true on success, false on failure
96      */
97     protected function purge()
98     {
99         $this->memcache->flush();
100     }
101 }