~ubuntu-branches/ubuntu/wily/php-horde-cache/wily

« back to all changes in this revision

Viewing changes to Horde_Cache-2.2.0/lib/Horde/Cache/Storage/Xcache.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-08-10 19:53:57 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20130810195357-empm8uhqxxb7vk5n
Tags: 2.2.1-1
New upstream version 2.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Copyright 2006-2013 Horde LLC (http://www.horde.org/)
4
 
 *
5
 
 * See the enclosed file COPYING for license information (LGPL). If you
6
 
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7
 
 *
8
 
 * @category  Horde
9
 
 * @copyright 2006-2013 Horde LLC
10
 
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11
 
 * @package   Cache
12
 
 */
13
 
 
14
 
/**
15
 
 * Cache storage in Xcache.
16
 
 *
17
 
 * @author    Duck <duck@obala.net>
18
 
 * @category  Horde
19
 
 * @copyright 2006-2013 Horde LLC
20
 
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
21
 
 * @package   Cache
22
 
 */
23
 
class Horde_Cache_Storage_Xcache extends Horde_Cache_Storage_Base
24
 
{
25
 
    /**
26
 
     * Constructor.
27
 
     *
28
 
     * @param array $params  Optional parameters:
29
 
     *   - prefix: (string) The prefix to use for the cache keys.
30
 
     *             DEFAULT: ''
31
 
     */
32
 
    public function __construct(array $params = array())
33
 
    {
34
 
        parent::__construct(array_merge(array(
35
 
            'prefix' => '',
36
 
        ), $params));
37
 
    }
38
 
 
39
 
    /**
40
 
     */
41
 
    public function get($key, $lifetime = 0)
42
 
    {
43
 
        $key = $this->_params['prefix'] . $key;
44
 
        $this->_setExpire($key, $lifetime);
45
 
        $result = xcache_get($key);
46
 
 
47
 
        return empty($result)
48
 
            ? false
49
 
            : $result;
50
 
    }
51
 
 
52
 
    /**
53
 
     */
54
 
    public function set($key, $data, $lifetime = 0)
55
 
    {
56
 
        $key = $this->_params['prefix'] . $key;
57
 
        if (xcache_set($key . '_expire', time(), $lifetime)) {
58
 
            xcache_set($key, $data, $lifetime);
59
 
        }
60
 
    }
61
 
 
62
 
    /**
63
 
     */
64
 
    public function exists($key, $lifetime = 0)
65
 
    {
66
 
        $key = $this->_params['prefix'] . $key;
67
 
        $this->_setExpire($key, $lifetime);
68
 
        return xcache_isset($key);
69
 
    }
70
 
 
71
 
    /**
72
 
     */
73
 
    public function expire($key)
74
 
    {
75
 
        $key = $this->_params['prefix'] . $key;
76
 
        xcache_unset($key . '_expire');
77
 
        return xcache_unset($key);
78
 
    }
79
 
 
80
 
    /**
81
 
     */
82
 
    public function clear()
83
 
    {
84
 
        // xcache_clear_cache() won't work because it requires HTTP Auth.
85
 
        throw new Horde_Cache_Exception('Not supported');
86
 
    }
87
 
 
88
 
    /**
89
 
     * Set expire time on each call since memcache sets it on cache creation.
90
 
     *
91
 
     * @param string $key        Cache key to expire.
92
 
     * @param integer $lifetime  Lifetime of the data in seconds.
93
 
     */
94
 
    protected function _setExpire($key, $lifetime)
95
 
    {
96
 
        if ($lifetime == 0) {
97
 
            // don't expire
98
 
            return;
99
 
        }
100
 
        $key = $this->_params['prefix'] . $key;
101
 
        $expire = xcache_get($key . '_expire');
102
 
 
103
 
        // set prune period
104
 
        if ($expire + $lifetime < time()) {
105
 
            // Expired
106
 
            xcache_unset($key . '_expire');
107
 
            xcache_unset($key);
108
 
        }
109
 
    }
110
 
 
111
 
}