thinkphp5的Redis缓存配置

thinkphp采用cache类提供缓存功能支持,采用驱动方式,在使用缓存之前需要进行初始化操作。支持的缓存类型包括file、memcache、wincache、sqlite、redis和xcache等,默认情况下是file类型,配置redis缓存可以单一配置redis也可以同时使用多个缓存类型。配置方式分别如下:

thinkphp采用cache类提供缓存功能支持,采用驱动方式,在使用缓存之前需要进行初始化操作。支持的缓存类型包括file、memcache、wincache、sqlite、redis和xcache等,默认情况下是file类型,配置redis缓存可以单一配置redis也可以同时使用多个缓存类型。配置方式分别如下:

一、仅配置redis缓存,在配置文件(app/config.php)中修改缓存设置如下:

二、配置多个缓存类型,使用符合缓存类型,配置方式如下:

‘cache‘ =>  [
        // 使用复合缓存类型
        ‘type‘  =>  ‘complex‘,
        // 默认使用的缓存
        ‘default‘   =>  [
            // 驱动方式
            ‘type‘   => ‘File‘,
            // 缓存保存目录
            ‘path‘   => CACHE_PATH,
        ],
        // 文件缓存
        ‘file‘   =>  [
            // 驱动方式
            ‘type‘   => ‘file‘,
            // 设置不同的缓存保存目录
            ‘path‘   => RUNTIME_PATH . ‘file/‘,
        ],
        // redis缓存
        ‘redis‘   =>  [
            // 驱动方式
            ‘type‘   => ‘redis‘,
            // 服务器地址
            ‘host‘       => ‘192.168.1.100‘,
        ],
    ],
使用符合缓存类型时,需要根据需要使用store方法切换缓存。

当使用

Cache::set(‘name‘, ‘value‘);
Cache::get(‘name‘);
的时候,使用的是default缓存标识的缓存配置。如果需要切换到其它的缓存标识操作,可以使用:

// 切换到file操作
Cache::store(‘file‘)->set(‘name‘,‘value‘);
Cache::get(‘name‘);
// 切换到redis操作
Cache::store(‘redis‘)->set(‘name‘,‘value‘);
Cache::get(‘name‘);
比如,查询一篇文章时首先从redis中查询,若未查到信息则从数据库中查询出结果,并存储到redis中。

---------------------
作者:AndreaDittis旦
来源:CSDN
原文:https://blog.csdn.net/u012835679/article/details/79900644
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/hellowzd/p/9814370.html

时间: 2024-08-03 10:03:24

thinkphp5的Redis缓存配置的相关文章

redis缓存配置

# Redis configuration file example # Note on units: when memory size is needed, it is possible to specifiy # it in the usual form of 1k 5GB 4M and so forth: # # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 byte

Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 package springboot01cache.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; im

22_redis缓存配置及设置把session存储在redis中

django配置redis缓存 1. 安装django-redis包 pip install -i https://pypi.douban.com/simple django-redis 2. 在settings.py 文件中,指定redis缓存 # 配置redis缓存 CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION":

springboot redis 缓存对象

只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile("org.springframework.boot:spring-boot-starter-data-redis:${springBootVersion}") redis configuration 中启用缓存 @Configuration @EnableCaching public class Re

SpringBoot使用Redis缓存

(1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> (2)application.properties中配置Redis连接信息,如下: # Redis数据库索引(默认为0) spring.red

yii配置 redis主从配置(附代码)

最近要搞redis 主从,但yii自带的redis 不支持主从,没办法只好自己写一个了 配置如下: 1.main.php中添加下面一句: //redis缓存配置 'cache_redis' => include(dirname(__FILE__) . DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'redis.php'), 2.redis.php文件如下 <?php return array( 'class'=>'ext.zredis.Y

spring boot 使用redis缓存

感谢大神分享! https://www.cnblogs.com/gdpuzxs/p/7222309.html (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> (2)修改项目启动类,增加注解@

Django的Session存储Redis环境配置

第一步:在项目目录下的settings.py中MIDDLEWARE中加上中间件: # session中间件Django项目默认启用Session 'django.contrib.sessions.middleware.SessionMiddleware', 第二步:在redis中保存session,需要引入第三方扩展,我们可以使用django-redis来解决. 安装扩展 pip install django-redis 第三步:在settings.py文件中做如下设置# redis缓存配置CA

spring-boot-2.0.3之redis缓存实现,不是你想的那样哦

前言 开心一刻 小白问小明:"你前面有一个5米深的坑,里面没有水,如果你跳进去后该怎样出来了?"小明:"躺着出来呗,还能怎么出来?"小白:"为什么躺着出来?"小明:"5米深的坑,还没有水,跳下去不死就很幸运了,残是肯定会残的,不躺着出来,那能怎么出来?"小白:"假设没死也没残呢?"小明:"你当我超人了? 那也简单,把脑子里的水放出来就可以漂出来了."小白:"你脑子里有这么多水吗