每天laravel-20160625|RedisStore

<?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.
}
时间: 2024-08-08 17:50:07

每天laravel-20160625|RedisStore的相关文章

Laravel 5.4 中的异常处理器和HTTP异常处理实例教程

错误和异常是处理程序开发中不可回避的议题,在本地开发中我们往往希望能捕获程序抛出的异常并将其显示打印出来,以便直观的知道程序在哪里出了问题并予以解决,而在线上环境我们不希望将程序错误或异常显示在浏览器中(出于安全考虑),这个时候我们仍然要捕获异常,只不过不是显示到浏览器中,而是记录到日志中,方便日后排查问题. 百牛信息技术bainiu.ltd整理发布于博客园 Laravel当然支持PHP原生的错误和异常处理,但是在此基础上进行了一些封装处理,从而更方便在不同开发环境切换以及对错误和异常的处理.

laravel框架数据迁移

迁移就像数据库的版本控制,允许团队简单轻松的编辑并共享应用的数据库表结构,迁移通常和Laravel 的 schema 构建器结对从而可以很容易地构建应用的数据库表结构.如果你曾经告知小组成员需要手动添加列到本地数据库结构,那么这正是数据库迁移所致力于解决的问题. Laravel 的 Schema 门面提供了与数据库系统无关的创建和操纵表的支持,在 Laravel 所支持的所有数据库系统中提供一致的.优雅的.平滑的 API. laravel默认有两个文件uses  和 password_reset

详细说明php的4中开源框架(TP,CI,Laravel,Yii)

ThinkPHP简称TP,TP借鉴了Java思想,基于PHP5,充分利用了PHP5的特性,部署简单只需要一个入口文件,一起搞定,简单高效.中文文档齐全,入门超级简单.自带模板引擎,具有独特的数据验证和自动填充功能,框架更新速度比较速度. 优点:这个框架易使用 易学 安全 对bae sae支持很好提供的工具也很强大 可以支持比较大的项目开发 易扩展 全中文文档 总的来说这款框架适合非常适合国人使用 性能 上比CI还要强一些 缺点:配置对有些人来说有些复杂(其实是因为没有认真的读过其框架源码)文档有

Laravel 5.0 - Middleware (中间件)

图片:http://stackphp.com/ 如上图所示,中心的绿色区域是整个应用的核心区域. 所以,中间件就是一系列处理请求和响应的方式而不是你用程序逻辑的一部分. Laravel 中默认使用中间件处理请求中的加密解密,以及 Cookies 和 Sessions.你也可以自定义自己所需的中间件. 写中间件 artisan make:middleware MyMiddleware 执行上面的命令,生成中间件文件: <?php namespace App\Http\Middleware; use

laravel安装笔记

一.安装composer 安装之前将\php\php.ini文件中的php_openssl.dll扩展库开启,否则composer在安装过程中会出现错误提示. (我在安装过程中发现apache目录下的php.ini最好也开启php_openssl.dll,就是讲前面的‘:’号去掉) composer下载地址:https://getcomposer.org/ windows下载地址:https://getcomposer.org/Composer-Setup.exe 二.下载Laravel最新框架

Laravel 5.5 的自定义验证对象/类

本文和大家分享的主要是Laravel 5.5 的自定义验证对象/类相关内容,一起来看看吧,希望对大家学习Laravel有所帮助. Laravel 5.5 将提供一个全新的自定义验证规则的对象,以作为原来的 Validator::extend 方法的替代. Laravel 中的表单验证是比较方便的,而且内置了大量的可用验证规则,但不管官方提供了多少,总还是会有满足不了需求的时候.很多时候我们会直接用正则表达式来处理这种特殊的验证,也有时候我们会选择用 Validator::extend来扩展一个自

laravel 数据库操作小例子

public function demo() { $res = null; //insert数据插入 //$user=array('username'=>'joy','password'=>'123456','age'=>23); //$res = DB::table('users')->insert($user); /* 数据查询 $res = DB::table('users')->where('username','joy')->get(); $res = DB:

Laravel中常见的错误与解决方法小结

一.报错: 「Can't swap PDO instance while within transaction」 通过查询 Laravel 源代码,可以确认异常是在 setPdo 方法中抛出的: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php public function setPdo($pdo) {   if ($this->transactions >= 1) {     throw new RuntimeException("

Laravel学习基础篇之--路由

终于还是决定再多学一门重量级框架,当然首选必备还是被称为最优雅的Web开发框架--Laravel 对于框架的入门,首先了解它的路由规则是先前必备的,以下是laravel 中几种常见的基础路由规则 //基础路由//GETRoute::get('basic',function (){ return 'Im,GET'; }); //POSTRoute::post('basic2',function (){ return 'Im,Post'; }); //多请求路由(两种方式:match & any m

小白也能看懂的 Laravel 核心概念讲解

自动依赖注入 什么是依赖注入,用大白话将通过类型提示的方式向函数传递参数. 实例 1 首先,定义一个类: /routes/web.php class Bar {} 假如我们在其他地方要使用到 Bar 提供的功能(服务),怎么办,直接传入参数即可: /routes/web.php Route::get('bar', function(Bar $bar) { dd($bar); }); 访问 /bar,显示 $bar 的实例: Bar {#272} 也就是说,我们不需要先对其进行实例!如果学过 PH