~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/Stack.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-10-08 18:54:29 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20131008185429-8r3ao97k5z15l325
Tags: 2.3.0-1
New upstream version 2.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Copyright 2010-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 2010-2013 Horde LLC
10
 
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11
 
 * @package   Cache
12
 
 */
13
 
 
14
 
/**
15
 
 * Driver that loops through a given list of storage drivers to search for a
16
 
 * cached value. Allows for use of caching backends on top of persistent
17
 
 * backends.
18
 
 *
19
 
 * @author    Michael Slusarz <slusarz@horde.org>
20
 
 * @category  Horde
21
 
 * @copyright 2010-2013 Horde LLC
22
 
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
23
 
 * @package   Cache
24
 
 */
25
 
class Horde_Cache_Storage_Stack extends Horde_Cache_Storage_Base
26
 
{
27
 
    /**
28
 
     * Stack of cache drivers.
29
 
     *
30
 
     * @var string
31
 
     */
32
 
    protected $_stack = array();
33
 
 
34
 
    /**
35
 
     * Constructor.
36
 
     *
37
 
     * @param array $params  Parameters:
38
 
     *   - stack: (array) [REQUIRED] An array of storage instances to loop
39
 
     *            through, in order of priority. The last entry is considered
40
 
     *            the 'master' driver, for purposes of writes.
41
 
     */
42
 
    public function __construct(array $params = array())
43
 
    {
44
 
        if (!isset($params['stack'])) {
45
 
            throw new InvalidArgumentException('Missing stack parameter.');
46
 
        }
47
 
 
48
 
        parent::__construct($params);
49
 
    }
50
 
 
51
 
    /**
52
 
     */
53
 
    protected function _initOb()
54
 
    {
55
 
        $this->_stack = $this->_params['stack'];
56
 
    }
57
 
 
58
 
    /**
59
 
     */
60
 
    public function get($key, $lifetime = 0)
61
 
    {
62
 
        foreach ($this->_stack as $val) {
63
 
            $result = $val->get($key, $lifetime);
64
 
            if ($result !== false) {
65
 
                return $result;
66
 
            }
67
 
        }
68
 
 
69
 
        return false;
70
 
    }
71
 
 
72
 
    /**
73
 
     */
74
 
    public function set($key, $data, $lifetime = 0)
75
 
    {
76
 
        /* Do writes in *reverse* order - it is OK if a write to one of the
77
 
         * non-master backends fails. */
78
 
        $master = true;
79
 
 
80
 
        foreach (array_reverse($this->_stack) as $val) {
81
 
            $result = $val->set($key, $data, $lifetime);
82
 
            if ($result === false) {
83
 
                if ($master) {
84
 
                    return;
85
 
                }
86
 
 
87
 
                /* Attempt to invalidate cache if write failed. */
88
 
                $val->expire($key);
89
 
            }
90
 
            $master = false;
91
 
        }
92
 
    }
93
 
 
94
 
    /**
95
 
     */
96
 
    public function exists($key, $lifetime = 0)
97
 
    {
98
 
        foreach ($this->_stack as $val) {
99
 
            $result = $val->exists($key, $lifetime);
100
 
            if ($result === true) {
101
 
                break;
102
 
            }
103
 
        }
104
 
 
105
 
        return $result;
106
 
    }
107
 
 
108
 
    /**
109
 
     */
110
 
    public function expire($key)
111
 
    {
112
 
        /* Only report success from master. */
113
 
        $master = $success = true;
114
 
 
115
 
        foreach (array_reverse($this->_stack) as $val) {
116
 
            $result = $val->expire($key);
117
 
            if ($master && ($result === false)) {
118
 
                $success = false;
119
 
            }
120
 
            $master = false;
121
 
        }
122
 
 
123
 
        return $success;
124
 
    }
125
 
 
126
 
    /**
127
 
     */
128
 
    public function clear()
129
 
    {
130
 
        /* Only report errors from master. */
131
 
        $exception = null;
132
 
        $master = true;
133
 
 
134
 
        foreach (array_reverse($this->_stack) as $val) {
135
 
            try {
136
 
                $val->clear();
137
 
            } catch (Horde_Cache_Exception $e) {
138
 
                if ($master) {
139
 
                    $exception = $e;
140
 
                }
141
 
            }
142
 
            $master = false;
143
 
        }
144
 
 
145
 
        if ($exception) {
146
 
            throw $exception;
147
 
        }
148
 
    }
149
 
 
150
 
}