laravel学习之路5缓存
2017-08-18 12:36
417 查看
redis需要先安装需要通过 Composer 安装 predis/predis 扩展包 (~1.0) 或者使用 PECL 安装 PhpRedis PHP 拓展。composer require predis/predisredis的配置在cache.php和database.php里面都有Redis 多连接的配置:https://laravel-china.org/topics/2466集群配置使用 clusters 键PhpRedisIf you have the PhpRedis PHP extension installed via PECL, you will need to rename the Redis alias in your config/app.php configuration file.Redis facade可以调用任何的 Redis 命令,如set, lrange注意不是Cache facadeRedis::set('key2', 'value2');//work管道化命令pipeline可以运行多个 命令发布与订阅https://redis.io/commands/publish php artisan redis:subscribeyour message is:{"foo1":"bar1"}使用Cache facade$value = Cache::get('key', 'default');刚开始取不到数据,是什么原因?//'prefix' => 'laravel','prefix' => '',如果不存在,就返回数据库里面的值$value = Cache::get('key', function () { return DB::table(...)->get(); });也可以使用全局函数cache('key');从缓存中取出所有用户,或者当用户不存在时,从数据库中将这些用户取出并放入缓存中,你可以使用 Cache::remember 方法实现:$value = Cache::remember('users', $minutes, function () { return DB::table('users')->get(); });获取一个缓存项然后删除它,可以使用 pull 方法手动在命令行里面设置的key,为何报unserialize"s:11:\"elesos1.com\";" 这个是程序存储的值elesos1.com直接用 Redis::set('elesos3', "elesos33"); return Redis::get('elesos3');不用序列号或cache存,cache取也不用(cache内部会自动序列化与反序列化)Cache::store('redis')->put('elesos4', 'elesos44', 10);return Cache::get('elesos4', 'no this key:1');但cache存储的在命令行中看是序列化后的数据: "s:8:\"elesos44\";"只有你redis存 cache取 或者反过来才需要序列化或反序列化如Redis::set('elesos6', serialize("elesos66"));return Cache::get('elesos6', 'no this key:1');//会自动反序列化Cache::store('redis')->put('elesos7', 'elesos77', 60); //60单位是分钟return Redis::get('elesos7'); // s:8:"elesos77";return unserialize(Redis::get('elesos7'));过期$expiresAt = Carbon::now()->addMinutes(10); Cache::put('key', 'value', $expiresAt);cache(['key' => 'value'], $minutes); cache(['key' => 'value'], Carbon::now()->addSeconds(10));Store If Not Present:add永久写入forever必须通过 forget 方法手动删除flush 方法清空所有缓存还可以监听缓存事件'Illuminate\Cache\Events\CacheHit' => [ 'App\Listeners\LogCacheHit', ],
相关文章推荐
- html5 学习之路 四 (应用缓存 、web worker 、服务器推送)
- 我的Laravel学习之路之环境配置
- 我的Laravel学习之路之我的Laravel学习之路之用户注册和登录 Auth
- laravel框架自带缓存学习,和一些缓存的区别理解
- 我的Laravel学习之路之migration Laravel自带命令行数据库创建和管理工具
- 我的Laravel学习之路之表单验证 validation
- laravel框架学习-缓存,事件
- [EntLib]微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)
- 我的Laravel学习之路之 模型方法queryScope 和字段设置setAttribute
- 学习之路三十四:再一次重构缓存设计
- 我的Laravel学习之路之Route::resource和Form-Model-Binding,
- laravel学习之路2: jwt集成
- 我的Laravel学习之路之模板加载,分配变量,简单路由
- [原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓存,时间戳缓存
- laravel学习之路2: jwt集成
- laravel框架学习之路(一)
- laravel学习之路3 数据库相关
- 微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)
- laravel学习之路3 数据库相关
- 我的Laravel学习之路之Eloquent 入门