memcached.php 1.46 KB
Newer Older
1
<?php namespace Laravel\Cache\Drivers; use Memcache;
Taylor Otwell committed
2 3 4

class Memcached extends Driver {

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

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

	/**
20 21 22 23 24
	 * Create a new Memcached cache driver instance.
	 *
	 * @param  Memcache  $memcache
	 * @return void
	 */
25
	public function __construct(Memcache $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
	{
Taylor Otwell committed
50 51 52 53
		if (($cache = $this->memcache->get($this->key.$key)) !== false)
		{
			return $cache;
		}
Taylor Otwell committed
54 55
	}

Taylor Otwell committed
56 57 58
	/**
	 * Write an item to the cache for a given number of minutes.
	 *
59 60 61 62 63
	 * <code>
	 *		// Put an item in the cache for 15 minutes
	 *		Cache::put('name', 'Taylor', 15);
	 * </code>
	 *
Taylor Otwell committed
64 65 66 67 68
	 * @param  string  $key
	 * @param  mixed   $value
	 * @param  int     $minutes
	 * @return void
	 */
Taylor Otwell committed
69 70
	public function put($key, $value, $minutes)
	{
71
		$this->memcache->set($this->key.$key, $value, 0, $minutes * 60);
Taylor Otwell committed
72 73
	}

Taylor Otwell committed
74 75 76 77 78 79
	/**
	 * Delete an item from the cache.
	 *
	 * @param  string  $key
	 * @return void
	 */
Taylor Otwell committed
80 81
	public function forget($key)
	{
82
		$this->memcache->delete($this->key.$key);
Taylor Otwell committed
83 84 85
	}

}