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);//设置
相关推荐
唐青枫7 小时前
别把后续动作全塞进控制器:Symfony 事件监听器与订阅者实战
php·symfony
新鲜势力呀8 小时前
PHP 消息队列实战:从同步接口阻塞到 RabbitMQ + Redis队列 + 异步任务架构完整优化方案
redis·rabbitmq·php
新鲜势力呀10 小时前
PHP 日志系统实战:从排查线上故障困难到 ELK日志分析 + 链路追踪 + 实时监控完整架构方案
elk·架构·php
事圆则缓14 小时前
Kotlin Flow、StateFlow、SharedFlow 全解析:冷流、热流与 Android 状态管理
android·kotlin·php
汤米粥15 小时前
后端开发主流技术方案
java·python·golang·php·nodejs·后端开发
在猴站学算法15 小时前
(实战)PHP文件包含漏洞
网络安全·php
谢亮_vipxieliang16 小时前
PHP 8 新特性:构造器属性提升与实战
开发语言·后端·php
Highcharts.js16 小时前
动态数据可视化架构:Highcharts + PHP + MySQL 从数据库到数据渲染
数据库·mysql·php·开发文档·实时数据·highcharts·图表开发
专业程序开发源16 小时前
springboot校园志愿者管理系统28562-计算机课程设计、毕业设计
java·spring boot·后端·python·django·php·课程设计
Days20501 天前
全网去水印1.0.0源码发布
php