Issues (3083)

smarty/demo/plugins/cacheresource.memcache.php (1 issue)

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
    /**
23
     * Smarty_CacheResource_Memcache constructor.
24
     */
25
    public function __construct()
26
    {
27
        if (class_exists('Memcached')) {
28
            $this->memcache = new Memcached();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Memcached() of type Memcached is incompatible with the declared type Memcache of property $memcache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        } else {
30
            $this->memcache = new Memcache();
31
        }
32
        $this->memcache->addServer('127.0.0.1', 11211);
33
    }
34
35
    /**
36
     * Read values for a set of keys from cache
37
     *
38
     * @param array $keys list of keys to fetch
39
     *
40
     * @return array   list of values with the given keys used as indexes
41
     * @return boolean true on success, false on failure
42
     */
43
    protected function read(array $keys)
44
    {
45
        $res = array();
46
        foreach ($keys as $key) {
47
            $k = sha1($key);
48
            $res[$key] = $this->memcache->get($k);
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
            if (class_exists('Memcached')) {
66
                $this->memcache->set($k, $v, $expire);
67
            } else {
68
                $this->memcache->set($k, $v, 0, $expire);
69
            }
70
        }
71
        return true;
72
    }
73
74
    /**
75
     * Remove values from cache
76
     *
77
     * @param array $keys list of keys to delete
78
     *
79
     * @return boolean true on success, false on failure
80
     */
81
    protected function delete(array $keys)
82
    {
83
        foreach ($keys as $k) {
84
            $k = sha1($k);
85
            $this->memcache->delete($k);
86
        }
87
        return true;
88
    }
89
90
    /**
91
     * Remove *all* values from cache
92
     *
93
     * @return boolean true on success, false on failure
94
     */
95
    protected function purge()
96
    {
97
        return $this->memcache->flush();
98
    }
99
}
100