<?php namespace Illuminate\Cache; use Memcached; use Illuminate\Contracts\Cache\Store; // a namespace about the user class MemcachedStore extends TaggableStore implements Store {// a Store about Memcache Store /** * The Memcached instance. * The Memcached instance. * @var \Memcached */ protected $memcached; /** * A string that should be prepended to keys. * A string that should be prepended to keys. * @var string */ protected $prefix;// a string about the prefix. with connection about the /** * Create a new Memcached store. * Create a new Memcached store. * @param \Memcached $memcached * @param string $prefix * @return void */ public function __construct($memcached, $prefix = '') { $this->setPrefix($prefix); $this->memcached = $memcached; }// a supper big prefix /** * Retrieve an item from the cache by key. * Retrieve an item form the cache by key. * @param string|array $key * @return mixed */ public function get($key) { $value = $this->memcached->get($this->prefix.$key);// a way to get value if ($this->memcached->getResultCode() == 0) { return $value; }// if return value, }// get a method /** * Retrieve multiple items from the cache by key. * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * Items not found in the cache will have a null value. * @param array $keys * @return array */ public function many(array $keys) { $prefixedKeys = array_map(function ($key) { return $this->prefix.$key; }, $keys);// you are master , $values = $this->memcached->getMulti($prefixedKeys, null, Memcached::GET_PRESERVE_ORDER); // more get if ($this->memcached->getResultCode() != 0) { return array_fill_keys($keys, null); }// get the result return array_combine($keys, $values); }// get a lot of keys /** * Store an item in the cache for a given number of minutes. * 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) { $this->memcached->set($this->prefix.$key, $value, $minutes * 60); }// a normal method ,put like push,set, /** * Store multiple items in the cache for a given number of 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) { $prefixedValues = [];// set a value array foreach ($values as $key => $value) { $prefixedValues[$this->prefix.$key] = $value; }// to control a new array t $this->memcached->setMulti($prefixedValues, $minutes * 60);// change time to second }// the method is putMany /** * Store an item in the cache if the key doesn't exist. * Store an item in the cache if the key doesn't exist. * @param string $key * @param mixed $value * @param int $minutes * @return bool */ public function add($key, $value, $minutes) { return $this->memcached->add($this->prefix.$key, $value, $minutes * 60); }// add ,increment , first to check,is has /** * Increment the value of an item in the cache. * Increment the value of an item in the cache. * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->memcached->increment($this->prefix.$key, $value); }// a wrapper to increment. /** * Decrement the value of an item in the cache. * Decrement the value of an item in the cache. * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->memcached->decrement($this->prefix.$key, $value); }// a wrapper to memcached to decrement /** * Store an item in the cache indefinitely. * Store an item in the cache indefinitely. * @param string $key * @param mixed $value * @return void */ public function forever($key, $value) { $this->put($key, $value, 0); }// forever to set a long time,never expired, ten 9 /** * Remove an item from the cache. * Remove an item from the cache. * @param string $key * @return bool */ public function forget($key) { return $this->memcached->delete($this->prefix.$key); }// ro make time to expired /** * Remove all items from the cache. * * @return void */ public function flush() { $this->memcached->flush(); }// remove all item from the cache. a flush to refresh /** * Get the underlying Memcached connection. * Get the underlying Memcached connection. * @return \Memcached */ public function getMemcached() { return $this->memcached; }// back the instance about the memcached /** * Get the cache key prefix. * Get the cache key prefix. * @return string */ public function getPrefix() { return $this->prefix; }// back a prefix /** * Set the cache key prefix. * Set the cache key prefix. * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = ! empty($prefix) ? $prefix.':' : ''; }// Set Prefix }
每天laravel-20160622|MeacachedStore
原创lijingsan1 博主文章分类:php ©著作权
文章标签 memcache warpper 文章分类 前端开发
©著作权归作者所有:来自51CTO博客作者lijingsan1的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章