每天laravel-20160701|Repository

来源:互联网 发布:罗替戈汀贴剂淘宝 编辑:程序博客网 时间:2024/05/21 23:48
<?phpnamespace Illuminate\Cache;use Closure;use DateTime;use ArrayAccess;use Carbon\Carbon;use BadMethodCallException;use Illuminate\Contracts\Cache\Store;use Illuminate\Support\Traits\Macroable;use Illuminate\Contracts\Events\Dispatcher;use Illuminate\Contracts\Cache\Repository as CacheContract;// a namespace be used enoughclass Repository implements CacheContract, ArrayAccess{// two interface that should be implements by Repository    use Macroable {        __call as macroCall;    }// use Macroable that we will be a __call    /**     * The cache store implementation.     * The cache store     * @var \Illuminate\Contracts\Cache\Store     */    protected $store;// a Cache Store instance of the implementation    /**     * The event dispatcher implementation.     *     * @var \Illuminate\Contracts\Events\Dispatcher     */    protected $events;// The event dispatcher implementation a events interface    /**     * The default number of minutes to store items.     *     * @var int     */    protected $default = 60; // a normal time[minutes]or[seconds] to set the value into the store items .    // normal set it is 60 second    /**     * Create a new cache repository instance.     *     * @param  \Illuminate\Contracts\Cache\Store  $store     * @return void     */    public function __construct(Store $store)    {        $this->store = $store;    }// a big __set to Create a new instance about Cache, repository    // a library to store.    /**     * Set the event dispatcher instance.     *     * @param  \Illuminate\Contracts\Events\Dispatcher  $events     * @return void     */    public function setEventDispatcher(Dispatcher $events)    {        $this->events = $events;    }// Set the event dispatcher instance.    // a big set to the events.    /**     * Fire an event for this cache instance.     *     * @param  string  $event     * @param  array  $payload     * @return void     */    // Fire an event for this cache instance.    protected function fireCacheEvent($event, $payload)// fire cache event    {        if (! isset($this->events)) {            return;        }// check this is a normal events        switch ($event) {// this->events is a instance about a class, then the events is a way or a type            case 'hit':// case hit:                if (count($payload) == 2) {                    $payload[] = [];                }                return $this->events->fire(new Events\CacheHit($payload[0], $payload[1], $payload[2]));            case 'missed':// case missed:                if (count($payload) == 1) {                    $payload[] = [];                }                return $this->events->fire(new Events\CacheMissed($payload[0], $payload[1]));            case 'delete':// case delete:                if (count($payload) == 1) {                    $payload[] = [];                }                return $this->events->fire(new Events\KeyForgotten($payload[0], $payload[1]));            case 'write':// cache write:                if (count($payload) == 3) {                    $payload[] = [];                }                return $this->events->fire(new Events\KeyWritten($payload[0], $payload[1], $payload[2], $payload[3]));        }    }// a switch way to change the result    /**     * Determine if an item exists in the cache.     *     * @param  string  $key     * @return bool     */    public function has($key)    {        return ! is_null($this->get($key));    }// determine[check] the item has exists in the cache.    /**     * Retrieve an item from the cache by key. Retrieve an item from the cache by key.     *     * @param  string  $key     * @param  mixed   $default     * @return mixed     */    public function get($key, $default = null)// get item by key.    {        if (is_array($key)) {            return $this->many($key);        }// the key is a array ,will back a item array by many.        $value = $this->store->get($this->itemKey($key));// get item[value] by key        if (is_null($value)) {// null the value            $this->fireCacheEvent('missed', [$key]);//missed            $value = value($default);        } else {            $this->fireCacheEvent('hit', [$key, $value]);// hit        }        return $value;    }    /**     * Retrieve multiple items from the cache by key.// Retrieve multiple items from the cache     *     * 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)    {        $normalizedKeys = [];        foreach ($keys as $key => $value) {            $normalizedKeys[] = is_string($key) ? $key : $value;        }        $values = $this->store->many($normalizedKeys);        foreach ($values as $key => &$value) {            if (is_null($value)) {                $this->fireCacheEvent('missed', [$key]);                $value = isset($keys[$key]) ? value($keys[$key]) : null;            } else {                $this->fireCacheEvent('hit', [$key, $value]);            }        }        return $values;    }    // 2016-04-05    /**     * Retrieve an item from the cache and delete it.     *     * @param  string  $key     * @param  mixed   $default     * @return mixed     */    public function pull($key, $default = null)// Retrieve an item from the cache and delete it.    {// like pop push , pull        $value = $this->get($key, $default);// first we will get value by key        $this->forget($key);// then forget the key, the same to delete the item by key        return $value;// return the key    }    /**     * Store an item in the cache.     *     * @param  string  $key     * @param  mixed   $value     * @param  \DateTime|int  $minutes     * @return void     */    public function put($key, $value, $minutes = null)// Store an item in the cache.    {        if (is_array($key) && filter_var($value, FILTER_VALIDATE_INT) !== false) {            return $this->putMany($key, $value);        }// if is a array and  check the filter var has int, the return putMany        $minutes = $this->getMinutes($minutes);// change minutes to you  wants        if (! is_null($minutes)) {// check has times [minutes]            $this->store->put($this->itemKey($key), $value, $minutes);// put key            $this->fireCacheEvent('write', [$key, $value, $minutes]);// fire cache event        }    }// to set a item in cache use a key    /**     * 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 item in the cache for a    {// given number of minutes        $minutes = $this->getMinutes($minutes);// get a normal time        if (! is_null($minutes)) {// has time            $this->store->putMany($values, $minutes);// use a api to set way            foreach ($values as $key => $value) {                $this->fireCacheEvent('write', [$key, $value, $minutes]);            }// a long a way to check the write event        }    }    /**     * Store an item in the cache if the key does not exist.     *     * @param  string  $key     * @param  mixed   $value     * @param  \DateTime|int  $minutes     * @return bool     */    public function add($key, $value, $minutes)// Store an item in the cache if the key does not exist.    {// add values by key in to the cache        $minutes = $this->getMinutes($minutes);// get a minutes        if (is_null($minutes)) {            return false;        }// no time no value        if (method_exists($this->store, 'add')) {            return $this->store->add($this->itemKey($key), $value, $minutes);        }// check has the add method, or done null        if (is_null($this->get($key))) {// no value            $this->put($key, $value, $minutes);// put a key and value            return true;        }        return false;    }    /**     * 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    {        $this->store->forever($this->itemKey($key), $value);// use api to set the key forever        $this->fireCacheEvent('write', [$key, $value, 0]);// send a event to    }    /**     * Get an item from the cache, or store the default value.     *     * @param  string  $key     * @param  \DateTime|int  $minutes     * @param  \Closure  $callback     * @return mixed     */    // Get an item from the cache ,or store the default value.    public function remember($key, $minutes, Closure $callback)    {        // If the item exists in the cache we will just return this immediately        // otherwise we will execute the given Closure and cache the result        // of that execution for the given number of minutes in storage.        if (! is_null($value = $this->get($key))) {            return $value;        }// has the item then back it        $this->put($key, $value = $callback(), $minutes);// set the        return $value;    }// a number to be remember    /**     * Get an item from the cache, or store the default value forever.     *     * @param  string   $key     * @param  \Closure  $callback     * @return mixed     */    public function sear($key, Closure $callback)    {        return $this->rememberForever($key, $callback);    }// Get an item from the cache, or store the default value forever    // like a 38 women, has the key ,i  use it,or i will set a default value,like kill it,    // even a forever    /**     * Get an item from the cache, or store the default value forever.     * Get an item from the cache, or store the     * @param  string   $key     * @param  \Closure  $callback     * @return mixed     */    // a method like sear ,means to remember Forever    public function rememberForever($key, Closure $callback)    {        // If the item exists in the cache we will just return this immediately        // otherwise we will execute the given Closure and cache the result        // of that execution for the given number of minutes. It's easy.        if (! is_null($value = $this->get($key))) {            return $value;        }        $this->forever($key, $value = $callback());        return $value;    }// if the item exists in the cache we will just return this immediately    // otherwise we will execute the given Closure and cache the result of that execution for    // the given of minutes ,it is easy    /**     * Remove an item from the cache.     *     * @param  string $key     * @return bool     */    public function forget($key)// delete or remove or forget an item from the cache    {        $success = $this->store->forget($this->itemKey($key));// a api to forget        $this->fireCacheEvent('delete', [$key]);// fire event  deltet        return $success;// get result, more time it is success!    }    /**     * Begin executing a new tags operation if the store supports it.     *     * @param  array|mixed  $names     * @return \Illuminate\Cache\TaggedCache     *     * @throws \BadMethodCallException     */    public function tags($names)// Begin executing a new tags operation if the store supports it.    {        if (method_exists($this->store, 'tags')) {// check has the method tags            $taggedCache = $this->store->tags($names);// change the name to            if (! is_null($this->events)) {                $taggedCache->setEventDispatcher($this->events);            }            $taggedCache->setDefaultCacheTime($this->default);            return $taggedCache;// set Default Cache then        }        throw new BadMethodCallException('This cache store does not support tagging.');    }    /**     * Format the key for a cache item.     *     * @param  string  $key     * @return string     */    protected function itemKey($key)    {        return $key;    }// Format the key for a cache item. but do nothing    /**     * Get the default cache time.     *     * @return int     */    public function getDefaultCacheTime()    {        return $this->default;// a default about time    }// Get the default cache time.    /**     * Set the default cache time in minutes.     *     * @param  int   $minutes     * @return void     */    public function setDefaultCacheTime($minutes)    {        $this->default = $minutes;    }// Set the default cache time in minutes.    /**     * Get the cache store implementation.     *     * @return \Illuminate\Contracts\Cache\Store     */    public function getStore()    {        return $this->store;// back an instance    }// get the cache store implementation.    /**     * Determine if a cached value exists.     *     * @param  string  $key     * @return bool     */    public function offsetExists($key)    {        return $this->has($key);// check has a key    }// Determine if a cached value exists    /**     * Retrieve an item from the cache by key.     *     * @param  string  $key     * @return mixed     */    public function offsetGet($key)    {        return $this->get($key);    }// Retrieve an item from the cache by key.    /**     * Store an item in the cache for the default time.     *     * @param  string  $key     * @param  mixed   $value     * @return void     */    public function offsetSet($key, $value)    {        $this->put($key, $value, $this->default);    }// Store an item in the    /**     * Remove an item from the cache.     *     * @param  string  $key     * @return void     */    public function offsetUnset($key)    {        $this->forget($key);    }// a remove an item from the cache. method    /**     * Calculate the number of minutes with the given duration.     *     * @param  \DateTime|int  $duration     * @return int|null     */    protected function getMinutes($duration)    {        if ($duration instanceof DateTime) {            $fromNow = Carbon::now()->diffInMinutes(Carbon::instance($duration), false);            return $fromNow > 0 ? $fromNow : null;        }        return is_string($duration) ? (int) $duration : $duration;    }// Calculate the number of minutes with the given duration.    // get time duration  for a int times    /**     * Handle dynamic calls into macros or pass missing methods to the store.     * Handle dynamic calls into macros or pass missing methods to the store.     * @param  string  $method     * @param  array   $parameters     * @return mixed     */    // dynamic calls into macros or pass missing methods    public function __call($method, $parameters)    {        if (static::hasMacro($method)) {            return $this->macroCall($method, $parameters);        }// static:: method to check has a method        return call_user_func_array([$this->store, $method], $parameters);    }// call_user_func_array()    /**     * Clone cache repository instance.     *     * @return void     */    public function __clone()    {        $this->store = clone $this->store;    }// Clone cache repository instance.}
0 0
原创粉丝点击