memory.php 2.53 KB
Newer Older
1 2
<?php namespace Laravel\Cache\Drivers;

3
class Memory extends Sectionable {
4 5 6 7 8 9

	/**
	 * The in-memory array of cached items.
	 *
	 * @var string
	 */
10
	public $storage = array();
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

	/**
	 * Determine if an item exists in the cache.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	public function has($key)
	{
		return ( ! is_null($this->get($key)));
	}

	/**
	 * Retrieve an item from the cache driver.
	 *
	 * @param  string  $key
	 * @return mixed
	 */
	protected function retrieve($key)
	{
31 32 33 34 35 36 37 38 39 40
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			return $this->get_from_section($section, $key);
		}
		else
		{
			return array_get($this->storage, $key);
		}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	}

	/**
	 * Write an item to the cache for a given number of minutes.
	 *
	 * <code>
	 *		// Put an item in the cache for 15 minutes
	 *		Cache::put('name', 'Taylor', 15);
	 * </code>
	 *
	 * @param  string  $key
	 * @param  mixed   $value
	 * @param  int     $minutes
	 * @return void
	 */
	public function put($key, $value, $minutes)
	{
58 59 60 61 62 63 64 65 66 67
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			return $this->put_in_section($section, $key, $value, $minutes);
		}
		else
		{
			array_set($this->storage, $key, $value);
		}
68 69 70
	}

	/**
71 72 73 74 75 76 77 78
	 * Write an item to the cache that lasts forever.
	 *
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
	public function forever($key, $value)
	{
79 80 81 82 83 84 85 86 87 88
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			return $this->forever_in_section($section, $key, $value);
		}
		else
		{
			$this->put($key, $value, 0);
		}
89 90 91
	}

	/**
92 93 94 95 96 97 98
	 * Delete an item from the cache.
	 *
	 * @param  string  $key
	 * @return void
	 */
	public function forget($key)
	{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			if ($key == '*')
			{
				$this->forget_section($section);
			}
			else
			{
				$this->forget_in_section($section, $key);
			}
		}
		else
		{
			array_forget($this->storage, $key);
		}
116 117 118 119 120 121 122 123 124 125 126
	}

	/**
	 * Delete an entire section from the cache.
	 *
	 * @param  string    $section
	 * @return int|bool
	 */
	public function forget_section($section)
	{
		array_forget($this->storage, 'section#'.$section);
127 128
	}

129 130 131 132 133 134 135
	/**
	 * Flush the entire cache.
	 *
	 * @return void
	 */
	public function flush()
	{
136
		$this->storage = array();
137 138
	}

139 140 141 142 143 144 145 146 147 148 149 150
	/**
	 * Get a section item key for a given section and key.
	 *
	 * @param  string  $section
	 * @param  string  $key
	 * @return string
	 */
	protected function section_item_key($section, $key)
	{
		return "section#{$section}.{$key}";
	}

151
}