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

« back to all changes in this revision

Viewing changes to Horde_Cache-2.5.0/lib/Horde/Cache/Storage/Apc.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-05-17 11:10:04 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140517111004-sivxqdkho9p34xfy
Tags: 2.5.0-1
New upstream version 2.5.0

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 the Alternative PHP Cache (APC).
 
16
 *
 
17
 * Copyright 2006-2014 Horde LLC (http://www.horde.org/)
 
18
 *
 
19
 * See the enclosed file COPYING for license information (LGPL). If you
 
20
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 
21
 *
 
22
 * @author    Duck <duck@obala.net>
 
23
 * @category  Horde
 
24
 * @copyright 2006-2014 Horde LLC
 
25
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
 
26
 * @package   Cache
 
27
 */
 
28
class Horde_Cache_Storage_Apc extends Horde_Cache_Storage_Base
 
29
{
 
30
    /**
 
31
     * Constructor.
 
32
     *
 
33
     * @param array $params  Optional parameters:
 
34
     * <pre>
 
35
     *   - prefix: (string) The prefix to use for the cache keys.
 
36
     *             DEFAULT: ''
 
37
     * </pre>
 
38
     */
 
39
    public function __construct(array $params = array())
 
40
    {
 
41
        parent::__construct(array_merge(array(
 
42
            'prefix' => '',
 
43
        ), $params));
 
44
    }
 
45
 
 
46
    /**
 
47
     */
 
48
    public function get($key, $lifetime = 0)
 
49
    {
 
50
        $key = $this->_params['prefix'] . $key;
 
51
        $this->_setExpire($key, $lifetime);
 
52
        return apc_fetch($key);
 
53
    }
 
54
 
 
55
    /**
 
56
     */
 
57
    public function set($key, $data, $lifetime = 0)
 
58
    {
 
59
        $key = $this->_params['prefix'] . $key;
 
60
        if (apc_store($key . '_expire', time(), $lifetime)) {
 
61
            apc_store($key, $data, $lifetime);
 
62
        }
 
63
    }
 
64
 
 
65
    /**
 
66
     */
 
67
    public function exists($key, $lifetime = 0)
 
68
    {
 
69
        $key = $this->_params['prefix'] . $key;
 
70
        $this->_setExpire($key, $lifetime);
 
71
        return (apc_fetch($key) !== false);
 
72
    }
 
73
 
 
74
    /**
 
75
     */
 
76
    public function expire($key)
 
77
    {
 
78
        $key = $this->_params['prefix'] . $key;
 
79
        apc_delete($key . '_expire');
 
80
        return apc_delete($key);
 
81
    }
 
82
 
 
83
    /**
 
84
     */
 
85
    public function clear()
 
86
    {
 
87
        if (!apc_clear_cache('user')) {
 
88
            throw new Horde_Cache_Exception('Clearing APC cache failed');
 
89
        }
 
90
    }
 
91
 
 
92
    /**
 
93
     * Set expire time on each call since APC sets it on 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 = apc_fetch($key . '_expire');
 
106
 
 
107
        // Set prune period.
 
108
        if ($expire + $lifetime < time()) {
 
109
            // Expired
 
110
            apc_delete($key);
 
111
            apc_delete($key . '_expire');
 
112
        }
 
113
    }
 
114
 
 
115
}