Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * Cache class that stores cached content in XCache.
13
 *
14
 * @package    symfony
15
 * @subpackage cache
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfXCacheCache.class.php 17865 2009-05-02 09:23:55Z FabianLange $
18
 */
19
class sfXCacheCache extends sfCache
20
{
21
  /**
22
   * Initializes this sfCache instance.
23
   *
24
   * Available options:
25
   *
26
   * * see sfCache for options available for all drivers
27
   *
28
   * @see sfCache
29
   */
30
  public function initialize($options = array())
31
  {
32
    parent::initialize($options);
33
 
34
    if (!function_exists('xcache_set'))
35
    {
36
      throw new sfInitializationException('You must have XCache installed and enabled to use sfXCacheCache class.');
37
    }
38
 
39
    if (!ini_get('xcache.var_size'))
40
    {
41
      throw new sfInitializationException('You must set the "xcache.var_size" variable to a value greater than 0 to use sfXCacheCache class.');
42
    }
43
  }
44
 
45
 /**
46
  * @see sfCache
47
  */
48
  public function get($key, $default = null)
49
  {
50
 
51
    $set = $this->getBaseValue($key);
52
 
53
    if (!is_array($set) || !array_key_exists('data', $set))
54
    {
55
 
56
      return $default;
57
    }
58
 
59
    return $set['data'];
60
  }
61
 
62
  /**
63
   * @see sfCache
64
   */
65
  public function has($key)
66
  {
67
    return xcache_isset($this->getOption('prefix').$key);
68
  }
69
 
70
  /**
71
   * @see sfCache
72
   */
73
  public function set($key, $data, $lifetime = null)
74
  {
75
    $lifetime = $this->getLifetime($lifetime);
76
 
77
    $set = array(
78
      'timeout' => time() + $lifetime,
79
      'data'    => $data,
80
      'ctime'   => time()
81
    );
82
 
83
    return xcache_set($this->getOption('prefix').$key, $set, $lifetime);
84
  }
85
 
86
  /**
87
   * @see sfCache
88
   */
89
  public function remove($key)
90
  {
91
    return xcache_unset($this->getOption('prefix').$key);
92
  }
93
 
94
  /**
95
   * @see sfCache
96
   */
97
  public function clean($mode = sfCache::ALL)
98
  {
99
    if ($mode !== sfCache::ALL)
100
    {
101
      return true;
102
    }
103
 
104
    $this->checkAuth();
105
 
106
    for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
107
    {
108
      if (false === xcache_clear_cache(XC_TYPE_VAR, $i))
109
      {
110
        return false;
111
      }
112
    }
113
 
114
    return true;
115
  }
116
 
117
  /**
118
   * @see sfCache
119
   */
120
  public function getLastModified($key)
121
  {
122
    $set = $this->getBaseValue($key);
123
 
124
    if (!is_array($set) || !array_key_exists('ctime', $set))
125
    {
126
 
127
      return 0;
128
    }
129
 
130
    return $set['ctime'];
131
  }
132
 
133
  /**
134
   * @see sfCache
135
   */
136
  public function getTimeout($key)
137
  {
138
 
139
    $set = $this->getBaseValue($key);
140
 
141
    if (!is_array($set) || !array_key_exists('timeout', $set))
142
    {
143
 
144
      return 0;
145
    }
146
 
147
    return $set['timeout'];
148
  }
149
 
150
  public function getBaseValue($key)
151
  {
152
    return xcache_isset($this->getOption('prefix').$key) ? xcache_get($this->getOption('prefix').$key) : null;
153
  }
154
 
155
  /**
156
   * @see sfCache
157
   */
158
  public function removePattern($pattern)
159
  {
160
    $this->checkAuth();
161
 
162
    $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
163
 
164
    for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
165
    {
166
      $infos = xcache_list(XC_TYPE_VAR, $i);
167
      if (!is_array($infos['cache_list']))
168
      {
169
        return;
170
      }
171
 
172
      foreach ($infos['cache_list'] as $info)
173
      {
174
        if (preg_match($regexp, $info['name']))
175
        {
176
          xcache_unset($info['name']);
177
        }
178
      }
179
    }
180
  }
181
 
182
  public function getCacheInfo($key)
183
  {
184
    $this->checkAuth();
185
 
186
    for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
187
    {
188
      $infos = xcache_list(XC_TYPE_VAR, $i);
189
 
190
      if (is_array($infos['cache_list']))
191
      {
192
        foreach ($infos['cache_list'] as $info)
193
        {
194
          if ($this->getOption('prefix').$key == $info['name'])
195
          {
196
            return $info;
197
          }
198
        }
199
      }
200
    }
201
 
202
    return null;
203
  }
204
 
205
  protected function checkAuth()
206
  {
207
    if (ini_get('xcache.admin.enable_auth'))
208
    {
209
      throw new sfConfigurationException('To use all features of the "sfXCacheCache" class, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.');
210
    }
211
  }
212
}