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

« back to all changes in this revision

Viewing changes to Horde_Cache-2.4.2/lib/Horde/Cache/Storage/Eaccelerator.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-04-13 09:11:33 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20140413091133-um8xxmlwuawkhbi5
Tags: 2.4.2-1
New upstream version 2.4.2

Show diffs side-by-side

added added

removed removed

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