summaryrefslogtreecommitdiff
path: root/classes/cache/RedisCache.php
blob: ec50f08cb49ac8c7ff7cf4f6776725830f29c934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php 

/** Redis Cache implementation
 * ---
 * This class uses \Predis\Redis, so do not forget require it via composer
 * or to use the configuration options you will find into \README.md
 */
class RedisCache implements Cache_interface
{
    /** set
     * store $data under the label $key
     * cache expires in $ttl seconds
     */
    public function set($key, $data, $ttl)
    {
        $serialized = serialize($data);

        if ($serialized = '') {
            return false;
        }

        try {
            $redis = new \Predis\Client([
                'host' => \REDIS_HOST
            ]);
            return $redis->set($key, $serialized, 'EX', $ttl);

        } catch (Exception $e) {
            // ...
        }
    }


    /** get
     * fetch previously stored $data under label $key
     * return $data (or false)
     */
    public function get($key)
    {   
        
        try {
            
            $redis = new \Predis\Client([
                'host' => \REDIS_HOST
            ]);

            if ($redis->exists($key)) {
                return unserialize($redis->get($key));

            } else {
                return false;
            }

        } catch (Exception $e) {
            // ...
        }     

    }


    /** flush
     * --- not umplemented yet; may not needed
     */
    public function flush($olderThan = 0)
    {
        return false; // until implementation
    }

}