企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一、redis介绍 redis 是一种nosql数据库,使用key-value存储数据,基于内存,所以redis也 可以用来做缓存 ## 二、redis与memcache区别 redis可以做持久化保存,memcache不可以。 32位,windows安装 适用php版本 5.5.12 下载地址:`http://pan.baidu.com/s/1skFlbBZ` 在php环境里配置扩展redis 用phpinfo函数测试redis是否成功。 ## 三、redis应用 先启动 `redis-server.exe` 然后打开 `redis-cli.exe` php链接redis代码: ~~~ <?php //如果是TP5需要加命名空间 $redis=new Redis(); $redis->connect("127.0.0.1",6379); //先设置,然后再把set注释输出get测试 //$redis->set('redistest',"测试redis"); echo $redis->get('redistest'); exit; ?> ~~~ ## 四、命令: 1,incr 功能:自增长数值。例: ~~~ incr count:2016:8:12:index:pv incr count:2016:8:12:user:108:index:pv ~~~ 作用:统计网站的访问量 2,setbit+bitcount 功能:统计。例: ~~~ setbit count:user:108 1 1 setbit count:user:108 2 1 setbit count:user:108 5 1 setbit count:user:108 20 1 setbit count:user:108 31 1 bitcount count:user:108 ~~~ 输出:5 作用:统计活跃度 3,setex 功能 摄制缓存时间。例: ~~~ setex name 5 zhangsan //设置name张三缓存5秒 get name //张三5秒后 获取不到数据 ~~~ 作用:替换php session 做用户的数据缓存方案 4,哈希 (1)hset hash 设置关联数组。例: ~~~ hset hash:user:187 username zhangsan hset hash:user:187 password 123456 hset hash:user:187 sex 1 hset hash:user:187 age 30 ~~~ (2)hget hash 获取单个关联数组值。例: ~~~ hget hash:user:187 age 30 ~~~ (3)hgetall hash 获取所有关联数组值.例: ~~~ hgetall hash:user:187 ~~~ hmset 同事设置多个关联数组值.例: ~~~ user:187 username zhangsan password 123456 age 30 sex 1 ~~~ ## 五、TP5应用: 1,引用缓存 先开启redis执行文件 ~~~ use think\Cache; ~~~ 2,配置链接 ~~~ $options = [ // 缓存类型为File 'type' => 'redis', // 缓存有效期为永久有效 'expire' => 7200, //缓存前缀 'prefix' => '' ]; $redis = Cache::connect($options); //设置redis $redis->set('type_list', $type_list); //获取redis $redis->get('type_list'); //清楚redis; $redis->rm('type_list'); ~~~