memcached.php 3.31 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
Sergii Grebeniuk committed
23
	 * @param  string     $key
24 25
	 * @return void
	 */
26
	public function __construct(\Memcached $memcache, $key)
27
	{
28
		$this->key = $key;
29 30 31 32
		$this->memcache = $memcache;
	}

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

Taylor Otwell committed
43 44 45 46 47 48 49
	/**
	 * Retrieve an item from the cache driver.
	 *
	 * @param  string  $key
	 * @return mixed
	 */
	protected function retrieve($key)
Taylor Otwell committed
50
	{
51 52 53 54 55 56 57
		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
58
		{
59
			return $cache;
Taylor Otwell committed
60
		}
Taylor Otwell committed
61 62
	}

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

Taylor Otwell committed
90
	/**
91 92 93 94 95 96 97 98
	 * Write an item to the cache that lasts forever.
	 *
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
	public function forever($key, $value)
	{
99 100 101 102 103 104 105 106 107 108
		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);
		}
109 110 111
	}

	/**
Taylor Otwell committed
112 113 114 115 116
	 * Delete an item from the cache.
	 *
	 * @param  string  $key
	 * @return void
	 */
Taylor Otwell committed
117 118
	public function forget($key)
	{
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		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
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 185
	/**
	 * 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
186
}