redis.php 1.54 KB
Newer Older
1 2 3 4 5 6 7
<?php namespace Laravel\Cache\Drivers;

class Redis extends Driver {

	/**
	 * The Redis database instance.
	 *
Phill Sparks committed
8
	 * @var Laravel\Redis
9 10 11 12 13 14
	 */
	protected $redis;

	/**
	 * Create a new Redis cache driver instance.
	 *
Phill Sparks committed
15
	 * @param  Laravel\Redis  $redis
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	 * @return void
	 */
	public function __construct(\Laravel\Redis $redis)
	{
		$this->redis = $redis;
	}

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

	/**
	 * Retrieve an item from the cache driver.
	 *
	 * @param  string  $key
	 * @return mixed
	 */
	protected function retrieve($key)
	{
		if ( ! is_null($cache = $this->redis->get($key)))
		{
			return unserialize($cache);
		}
	}

	/**
	 * 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)
	{
63
		$this->forever($key, $value);
64 65 66 67 68

		$this->redis->expire($key, $minutes * 60);
	}

	/**
69 70 71 72 73 74 75 76 77 78 79 80
	 * Write an item to the cache that lasts forever.
	 *
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
	public function forever($key, $value)
	{
		$this->redis->set($key, serialize($value));
	}

	/**
81 82 83 84 85 86 87 88 89 90 91
	 * Delete an item from the cache.
	 *
	 * @param  string  $key
	 * @return void
	 */
	public function forget($key)
	{
		$this->redis->del($key);
	}

}