您的位置:首页 > 数据库 > Redis

每天laravel-20160625|RedisStore

2016-04-02 22:28 681 查看
<?php

namespace Illuminate\Cache;

use Illuminate\Contracts\Cache\Store;
use Illuminate\Redis\Database as Redis;
// a namespace to enough
class RedisStore extends TaggableStore implements Store
{// a Store about use in Redis
/**
* The Redis database connection.
*
* @var \Illuminate\Redis\Database
*/
protected $redis;// The Redis database connection,it is a connection instance
// a instance about redis

/**
* A string that should be prepended to keys.
*
* @var string
*/
protected $prefix;// a prefix key, it is a string that should be prepended to keys.

/**
* The Redis connection that should be used.
*
* @var string
*/
protected $connection;// a connection to redis,
// The Redis connection that should be used.

/**
* Create a new Redis store.
*
* @param  \Illuminate\Redis\Database  $redis
* @param  string  $prefix
* @param  string  $connection
* @return void
*/
public function __construct(Redis $redis, $prefix = '', $connection = 'default')
{
$this->redis = $redis;
$this->setPrefix($prefix);
$this->connection = $connection;
}// Create a new Redis store.
// set a big data, new a instance, set a prefix, a connection way or a type

/**
* Retrieve an item from the cache by key.
*
* @param  string|array  $key
* @return mixed
*/
public function get($key)
{
if (! is_null($value = $this->connection()->get($this->prefix.$key))) {
return is_numeric($value) ? $value : unserialize($value);
}
}// Retrieve an item from the cache by key.
// get value by key.

/**
* Retrieve multiple items from the cache by key.
* get many items by key. from the cache
* Items not found in the cache will have a null value.
* can't found , so  back a null value.
* @param  array  $keys
* @return array
*/
public function many(array $keys)
{
$return = [];// a store array

$prefixedKeys = array_map(function ($key) {
return $this->prefix.$key;
}, $keys);// get keys by key and prefix from the array_map method.

$values = $this->connection()->mget($prefixedKeys);// get a value by keys array

foreach ($values as $index => $value) {
$return[$keys[$index]] = is_numeric($value) ? $value : unserialize($value);
}// reset $values to $return , check is a number

return $return;
}// get a lots of value from cache by keys.

/**
* Store an item in the cache for a given number of minutes.
*
* @param  string  $key
* @param  mixed   $value
* @param  int     $minutes
* @return void
*/
public function put($key, $value, $minutes)// Store an item in the cache for a given number of minutes
{
$value = is_numeric($value) ? $value : serialize($value);// set number or string

$minutes = max(1, $minutes);// set minutes not a second

$this->connection()->setex($this->prefix.$key, $minutes * 60, $value);// use a api function
}// put a values to cache minutes

/**
* Store multiple items in the cache for a given number of minutes.
*
* @param  array  $values
* @param  int  $minutes
* @return void
*/
public function putMany(array $values, $minutes)// Store multiple items in the cache for a given number of minutes.
{
$this->connection()->multi();// set multi method

foreach ($values as $key => $value) {
$this->put($key, $value, $minutes);// use a put method
}

$this->connection()->exec();// to  done
}

/**
* Increment the value of an item in the cache.
*
* @param  string  $key
* @param  mixed   $value
* @return int
*/
public function increment($key, $value = 1)// Increment the value of an item in the cache.
{
return $this->connection()->incrby($this->prefix.$key, $value);
}// use api to increment

/**
* Decrement the value of an item in the cache.
*
* @param  string  $key
* @param  mixed   $value
* @return int
*/
public function decrement($key, $value = 1)// Decrement the value of an item in the cache.
{
return $this->connection()->decrby($this->prefix.$key, $value);// a de crby  is a api
}

/**
* Store an item in the cache indefinitely.
*
* @param  string  $key
* @param  mixed   $value
* @return void
*/
public function forever($key, $value)// Store an item in the cache indefinitely.
{
$value = is_numeric($value) ? $value : serialize($value);// $value is check

$this->connection()->set($this->prefix.$key, $value);// set a keys and values
}

/**
* Remove an item from the cache.
*
* @param  string  $key
* @return bool
*/
public function forget($key)
{
return (bool) $this->connection()->del($this->prefix.$key);
}// Remove an item from the cache. use a api method del

/**
* Remove all items from the cache.
*
* @return void
*/
public function flush()
{
$this->connection()->flushdb();// use api
}// Remove all items from the cache.

/**
* Begin executing a new tags operation.
*
* @param  array|mixed  $names
* @return \Illuminate\Cache\RedisTaggedCache
*/
public function tags($names)// begin executing a new tags
{
return new RedisTaggedCache($this, new TagSet($this, is_array($names) ? $names : func_get_args()));
}// a new tags

/**
* Get the Redis connection instance.
*
* @return \Predis\ClientInterface
*/
public function connection()// Get the redis connection instance.
{
return $this->redis->connection($this->connection);// this is a connection to connection redis
}

/**
* Set the connection name to be used.
*
* @param  string  $connection
* @return void
*/
public function setConnection($connection)
{
$this->connection = $connection;
}// Set the connection name to be used. a big set

/**
* Get the Redis database instance.
*
* @return \Illuminate\Redis\Database
*/
public function getRedis()
{
return $this->redis;
}// Get the Redis database instance.
// a big __get

/**
* Get the cache key prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}// Get the cache key prefix.

/**
* Set the cache key prefix.
*
* @param  string  $prefix
* @return void
*/
public function setPrefix($prefix)
{
$this->prefix = ! empty($prefix) ? $prefix.':' : '';
}// Set the cache key prefix.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: