laravel cache

一、基本操作

php 复制代码
Cache::put() 创建缓存(键,值,有效期(单位是秒))
Cache::get() 获取缓存
Cache::add() 只会在缓存项不存在的情况下添加数据到缓存,如果数据被成功返回 true,否则,返回 false
Cache::pull() 从缓存中获取缓存项然后删除,使用 pull 方法,如果缓存项不存在的话返回 null
Cache::forever() 用于持久化存储数据到缓存,这些值必须通过 forget 方法手动从缓存中移除
Cache::forget() 从缓存中移除缓存项数据
Cache::has() 用于判断缓存项是否存在,如果值为 null 或 false 该方法会返回 false

二、基本配置

laravel框架的缓存配置在config/cache.php文件中

1)、配置文件

php 复制代码
<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Cache Store
    |--------------------------------------------------------------------------
    |
    | This option controls the default cache connection that gets used while
    | using this caching library. This connection is used when another is
    | not explicitly specified when executing a given caching function.
    |
    | Supported: "apc", "array", "database", "file",
    |            "memcached", "redis", "dynamodb"
    |
    */

    'default' => env('CACHE_DRIVER', 'file'),

    /*
    |--------------------------------------------------------------------------
    | Cache Stores
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the cache "stores" for your application as
    | well as their drivers. You may even define multiple stores for the
    | same cache driver to group types of items stored in your caches.
    |
    */

    'stores' => [

        'apc' => [
            'driver' => 'apc',
        ],

        'array' => [
            'driver' => 'array',
            'serialize' => false,
        ],

        'database' => [
            'driver' => 'database',
            'table' => 'cache',
            'connection' => null,
        ],

        'file' => [
            'driver' => 'file',
            'path' => storage_path('framework/cache/data'),
        ],

        'memcached' => [
            'driver' => 'memcached',
            'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
            'sasl' => [
                env('MEMCACHED_USERNAME'),
                env('MEMCACHED_PASSWORD'),
            ],
            'options' => [
                // Memcached::OPT_CONNECT_TIMEOUT => 2000,
            ],
            'servers' => [
                [
                    'host' => env('MEMCACHED_HOST', '127.0.0.1'),
                    'port' => env('MEMCACHED_PORT', 11211),
                    'weight' => 100,
                ],
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'cache',
        ],

        'dynamodb' => [
            'driver' => 'dynamodb',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
            'table' => env('DYNAMODB_CACHE_TABLE', 'cache'),
            'endpoint' => env('DYNAMODB_ENDPOINT'),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Cache Key Prefix
    |--------------------------------------------------------------------------
    |
    | When utilizing a RAM based store such as APC or Memcached, there might
    | be other applications utilizing the same cache. So, we'll specify a
    | value to get prefixed to all our keys so we can avoid collisions.
    |
    */

    'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'),

];

2)、缓存驱动

file - 将 Session 保存在 framework/cache/data 中。

cookie - Session 保存在安全加密的 Cookie 中。

database - Session 保存在关系型数据库中。

memcached / redis - Sessions 保存在其中一个快速且基于缓存的存储系统中。

array - Sessions 保存在 PHP 数组中,不会被持久化。

三、缓存操作

1)、设置缓存

php 复制代码
Cache::put('key', 'value', $minutes);

//将不存在于缓存中的数据放入缓存中,如果存放成功返回 true ,否则返回 false
Cache::add('key', 'value', $minutes);

//数据永久存入缓存
Cache::forever('key', 'value');

//获取users缓存,如果不存在,执行第三个参数,将返回值存入缓存
$value = Cache::remember('userinfo', $minutes, function () {
    return DB::table('userinfo')->get();
});

//获取users缓存,如果不存在,执行第三个参数,将返回值存入缓存并永久储存
$value = Cache::rememberForever('userinfo', function() {
    return DB::table('userinfo')->get();
});

2)、获取缓存

php 复制代码
$value = Cache::get('key')
//传递第二个参数,用来指定如果查找的数据不存在时,你希望返回的默认值
$value = Cache::get('key', 'default');
//第二个参数传递 Closure 作为默认值。如果指定的数据不存在于缓存中,将返回 Closure 的结果
$value = Cache::get('key', function () {
    return DB::table(...)->get();
});

3)、判断缓存是否存在

php 复制代码
//如果值为 null 或 不存在返回false
Cache::has('key')

4)、递增递减

php 复制代码
Cache::increment('key');
Cache::increment('key', $num);
Cache::decrement('key');
Cache::decrement('key', $num);

5)、删除

php 复制代码
//从缓存中获取到数据之后再删除它,如果缓存中不存在该数据, 则返回 null
$value = Cache::pull('key');
//删除缓存
Cache::forget('key');
//清空缓存
Cache::flush();

6)、使用多种缓存

php 复制代码
$value = Cache::store('file')->get('foo');//获取
Cache::store('redis')->put('bar', 'baz', 10);//设置
相关推荐
原机小子26 分钟前
Spring Boot框架下的新闻推荐技术
服务器·spring boot·php
VXbishe1 小时前
(附源码)基于springboot的“我来找房”微信小程序的设计与实现-计算机毕设 23157
java·python·微信小程序·node.js·c#·php·课程设计
柏箱4 小时前
PHP基本语法总结
开发语言·前端·html·php
tekin6 小时前
macos 中使用macport安装,配置,切换多版本php,使用port 安装php扩展方法总结
开发语言·macos·php·port·mac多版本php安装管理·port-select
火红的小辣椒7 小时前
PHP反序列化7(字符串逃逸)
开发语言·web安全·php
计算机源码社8 小时前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
火红的小辣椒8 小时前
PHP反序列化8(phar反序列化)
开发语言·web安全·php
荻酷社区14 小时前
子比主题美化 – 添加天气教程
php
2401_8576226615 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
小鹿( ﹡ˆoˆ﹡ )17 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php