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 EAccelerator.
13
 *
14
 * @package    symfony
15
 * @subpackage cache
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfEAcceleratorCache.class.php 21908 2009-09-11 12:06:21Z fabien $
18
 */
19
class sfEAcceleratorCache 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('eaccelerator_put') || !ini_get('eaccelerator.enable'))
35
    {
36
      throw new sfInitializationException('You must have EAccelerator installed and enabled to use sfEAcceleratorCache class (or perhaps you forgot to add --with-eaccelerator-shared-memory when installing).');
37
    }
38
  }
39
 
40
 /**
41
  * @see sfCache
42
  */
43
  public function get($key, $default = null)
44
  {
45
    $value = eaccelerator_get($this->getOption('prefix').$key);
46
 
47
    return null === $value ? $default : $value;
48
  }
49
 
50
  /**
51
   * @see sfCache
52
   */
53
  public function has($key)
54
  {
55
    return null !== eaccelerator_get($this->getOption('prefix'.$key));
56
  }
57
 
58
  /**
59
   * @see sfCache
60
   */
61
  public function set($key, $data, $lifetime = null)
62
  {
63
    return eaccelerator_put($this->getOption('prefix').$key, $data, $this->getLifetime($lifetime));
64
  }
65
 
66
  /**
67
   * @see sfCache
68
   */
69
  public function remove($key)
70
  {
71
    return eaccelerator_rm($this->getOption('prefix').$key);
72
  }
73
 
74
  /**
75
   * @see sfCache
76
   */
77
  public function removePattern($pattern)
78
  {
79
    $infos = eaccelerator_list_keys();
80
 
81
    if (is_array($infos))
82
    {
83
      $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
84
 
85
      foreach ($infos as $info)
86
      {
87
        if (preg_match($regexp, $info['name']))
88
        {
89
          eaccelerator_rm($this->getOption('prefix').$key);
90
        }
91
      }
92
    }
93
  }
94
 
95
  /**
96
   * @see sfCache
97
   */
98
  public function clean($mode = sfCache::ALL)
99
  {
100
    if (sfCache::OLD === $mode)
101
    {
102
      return eaccelerator_gc();
103
    }
104
 
105
    $infos = eaccelerator_list_keys();
106
    if (is_array($infos))
107
    {
108
      foreach ($infos as $info)
109
      {
110
        if (false !== strpos($info['name'], $this->getOption('prefix')))
111
        {
112
          // eaccelerator bug (http://eaccelerator.net/ticket/287)
113
          $key = 0 === strpos($info['name'], ':') ? substr($info['name'], 1) : $info['name'];
114
          if (!eaccelerator_rm($key))
115
          {
116
            return false;
117
          }
118
        }
119
      }
120
    }
121
 
122
    return true;
123
  }
124
 
125
  /**
126
   * @see sfCache
127
   */
128
  public function getLastModified($key)
129
  {
130
    if ($info = $this->getCacheInfo($key))
131
    {
132
      return $info['created'];
133
    }
134
 
135
    return 0;
136
  }
137
 
138
  /**
139
   * @see sfCache
140
   */
141
  public function getTimeout($key)
142
  {
143
    if ($info = $this->getCacheInfo($key))
144
    {
145
      return -1 == $info['ttl'] ? 0 : $info['created'] + $info['ttl'];
146
    }
147
 
148
    return 0;
149
  }
150
 
151
  protected function getCacheInfo($key)
152
  {
153
    $infos = eaccelerator_list_keys();
154
 
155
    if (is_array($infos))
156
    {
157
      foreach ($infos as $info)
158
      {
159
        if ($this->getOption('prefix').$key == $info['name'])
160
        {
161
          return $info;
162
        }
163
      }
164
    }
165
 
166
    return null;
167
  }
168
}