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

« back to all changes in this revision

Viewing changes to Horde_Cache-2.2.1/lib/Horde/Cache/Storage/Eaccelerator.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 eAccelerator (version 0.9.5+).
 
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_Eaccelerator 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
     * @throws Horde_Cache_Exception
 
33
     */
 
34
    public function __construct(array $params = array())
 
35
    {
 
36
        if (!function_exists('eaccelerator_gc')) {
 
37
            throw new Horde_Cache_Exception('eAccelerator must be compiled with support for shared memory to use as caching backend.');
 
38
        }
 
39
 
 
40
        parent::__construct(array_merge(array(
 
41
            'prefix' => '',
 
42
        ), $params));
 
43
    }
 
44
 
 
45
    /**
 
46
     */
 
47
    public function get($key, $lifetime = 0)
 
48
    {
 
49
        $key = $this->_params['prefix'] . $key;
 
50
        $this->_setExpire($key, $lifetime);
 
51
        return eaccelerator_get($key);
 
52
    }
 
53
 
 
54
    /**
 
55
     */
 
56
    public function set($key, $data, $lifetime = 0)
 
57
    {
 
58
        $key = $this->_params['prefix'] . $key;
 
59
        if (eaccelerator_put($key . '_expire', time(), $lifetime)) {
 
60
            eaccelerator_put($key, $data, $lifetime);
 
61
        }
 
62
    }
 
63
 
 
64
    /**
 
65
     */
 
66
    public function exists($key, $lifetime = 0)
 
67
    {
 
68
        $key = $this->_params['prefix'] . $key;
 
69
        $this->_setExpire($key, $lifetime);
 
70
        return eaccelerator_get($key) !== false;
 
71
    }
 
72
 
 
73
    /**
 
74
     */
 
75
    public function expire($key)
 
76
    {
 
77
        $key = $this->_params['prefix'] . $key;
 
78
        eaccelerator_rm($key . '_expire');
 
79
        return eaccelerator_rm($key);
 
80
    }
 
81
 
 
82
    /**
 
83
     */
 
84
    public function clear()
 
85
    {
 
86
        eaccelerator_clear();
 
87
    }
 
88
 
 
89
    /**
 
90
     * Set expire time on each call since eAccelerator sets it on
 
91
     * cache creation.
 
92
     *
 
93
     * @param string $key        Cache key to expire.
 
94
     * @param integer $lifetime  Lifetime of the data in seconds.
 
95
     */
 
96
    protected function _setExpire($key, $lifetime)
 
97
    {
 
98
        if ($lifetime == 0) {
 
99
            // Don't expire.
 
100
            return;
 
101
        }
 
102
 
 
103
        $key = $this->_params['prefix'] . $key;
 
104
        $expire = eaccelerator_get($key . '_expire');
 
105
 
 
106
        // Set prune period.
 
107
        if ($expire + $lifetime < time()) {
 
108
            // Expired
 
109
            eaccelerator_rm($key);
 
110
            eaccelerator_rm($key . '_expire');
 
111
        }
 
112
    }
 
113
 
 
114
}