memcached.php 3.28 KB
Newer Older
1
<?php namespace Laravel\Cache\Drivers;
Taylor Otwell committed
2

3
class Memcached extends Sectionable {
Taylor Otwell committed
4

Taylor Otwell committed
5
	/**
6 7
	 * The Memcache instance.
	 *
8
	 * @var Memcached
9
	 */
10
	public $memcache;
11 12

	/**
13 14 15 16
	 * The cache key from the cache configuration file.
	 *
	 * @var string
	 */
17
	protected $key;
18 19

	/**
20 21
	 * Create a new Memcached cache driver instance.
	 *
22
	 * @param  Memcached  $memcache
23 24
	 * @return void
	 */
25
	public function __construct(\Memcached $memcache, $key)
26
	{
27
		$this->key = $key;
28 29 30 31
		$this->memcache = $memcache;
	}

	/**
Taylor Otwell committed
32 33 34 35 36
	 * Determine if an item exists in the cache.
	 *
	 * @param  string  $key
	 * @return bool
	 */
Taylor Otwell committed
37 38 39 40 41
	public function has($key)
	{
		return ( ! is_null($this->get($key)));
	}

Taylor Otwell committed
42 43 44 45 46 47 48
	/**
	 * Retrieve an item from the cache driver.
	 *
	 * @param  string  $key
	 * @return mixed
	 */
	protected function retrieve($key)
Taylor Otwell committed
49
	{
50 51 52 53 54 55 56
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			return $this->get_from_section($section, $key);
		}
		elseif (($cache = $this->memcache->get($this->key.$key)) !== false)
Taylor Otwell committed
57
		{
58
			return $cache;
Taylor Otwell committed
59
		}
Taylor Otwell committed
60 61
	}

Taylor Otwell committed
62 63 64
	/**
	 * Write an item to the cache for a given number of minutes.
	 *
65 66 67 68 69
	 * <code>
	 *		// Put an item in the cache for 15 minutes
	 *		Cache::put('name', 'Taylor', 15);
	 * </code>
	 *
Taylor Otwell committed
70 71 72 73 74
	 * @param  string  $key
	 * @param  mixed   $value
	 * @param  int     $minutes
	 * @return void
	 */
Taylor Otwell committed
75 76
	public function put($key, $value, $minutes)
	{
77 78 79 80 81 82 83 84 85 86
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			return $this->put_in_section($section, $key, $value, $minutes);
		}
		else
		{
			$this->memcache->set($this->key.$key, $value, $minutes * 60);
		}
Taylor Otwell committed
87 88
	}

Taylor Otwell committed
89
	/**
90 91 92 93 94 95 96 97
	 * Write an item to the cache that lasts forever.
	 *
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
	public function forever($key, $value)
	{
98 99 100 101 102 103 104 105 106 107
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			return $this->forever_in_section($section, $key, $value);
		}
		else
		{
			return $this->put($key, $value, 0);
		}
108 109 110
	}

	/**
Taylor Otwell committed
111 112 113 114 115
	 * Delete an item from the cache.
	 *
	 * @param  string  $key
	 * @return void
	 */
Taylor Otwell committed
116 117
	public function forget($key)
	{
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		if ($this->sectionable($key))
		{
			list($section, $key) = $this->parse($key);

			if ($key == '*')
			{
				$this->forget_section($section);
			}
			else
			{
				$this->forget_in_section($section, $key);
			}
		}
		else
		{
			$this->memcache->delete($this->key.$key);
		}
Taylor Otwell committed
135 136
	}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	/**
	 * Delete an entire section from the cache.
	 *
	 * @param  string    $section
	 * @return int|bool
	 */
	public function forget_section($section)
	{
		return $this->memcache->increment($this->key.$this->section_key($section));
	}

	/**
	 * Get the current section ID for a given section.
	 *
	 * @param  string  $section
	 * @return int
	 */
	protected function section_id($section)
	{
		return $this->sear($this->section_key($section), function()
		{
			return rand(1, 10000);
		});
	}

	/**
	 * Get a section key name for a given section.
	 *
	 * @param  string  $section
	 * @return string
	 */
	protected function section_key($section)
	{
		return $section.'_section_key';
	}

	/**
	 * 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.'#'.$this->section_id($section).'#'.$key;
	}

Taylor Otwell committed
185
}