1<?php
2
3/**
4 * Swift Mailer Native memory runtime cache
5 * Please read the LICENSE file
6 * @author Chris Corbyn <chris@w3style.co.uk>
7 * @package Swift_Cache
8 * @license GNU Lesser General Public License
9 */
10
11require_once dirname(__FILE__) . "/../ClassLoader.php";
12Swift_ClassLoader::load("Swift_Cache");
13
14/**
15 * Caches data in variables - uses memory!
16 * @package Swift_Cache
17 * @author Chris Corbyn <chris@w3style.co.uk>
18 */
19class Swift_Cache_Memory extends Swift_Cache
20{
21  /**
22   * The storage container for this cache
23   * @var array
24   */
25  protected $store = array();
26  /**
27   * The key which was last requested
28   * @var string
29   */
30  protected $requested;
31
32  /**
33   * Write data to the cache
34   * @param string The cache key
35   * @param string The data to write
36   */
37  public function write($key, $data)
38  {
39    if (!isset($this->store[$key])) $this->store[$key] = $data;
40    else $this->store[$key] .= $data;
41  }
42  /**
43   * Clear the cached data (unset)
44   * @param string The cache key
45   */
46  public function clear($key)
47  {
48    $this->store[$key] = null;
49    unset($this->store[$key]);
50  }
51  /**
52   * Check if data is cached for $key
53   * @param string The cache key
54   * @return boolean
55   */
56  public function has($key)
57  {
58    return array_key_exists($key, $this->store);
59  }
60  /**
61   * Read data from the cache for $key
62   * It makes no difference to memory/speed if data is read in chunks so arguments are ignored
63   * @param string The cache key
64   * @return string
65   */
66  public function read($key, $size=null)
67  {
68    if (!$this->has($key)) return false;
69
70    if ($this->requested == $key)
71    {
72      $this->requested = null;
73      return false;
74    }
75    $this->requested = $key;
76    return $this->store[$key];
77  }
78}
79