浅谈php的缓存机制之redis
2015-07-19 00:00
627 查看
适合刚学习redis的读读,直接上代码吧,全是基础,全有注释
联系qq:274501366
代码很基础,转载请注明。
版权声明:本文为博主原创文章,未经博主允许不得转载。
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); //设置twjteststring $redis->setex('twjteststring','300','test string title content'); $twjteststring = $redis->get('twjteststring'); echo 'string<br/>'; print_r($twjteststring); echo "<hr/><br/>"; //设置hash echo 'hash<br/>'; $redis->hSet('testhash','name','twj'); $redis->hSet('testhash','age',25); //删除testhash中的age $redis->hDel('testhash','age'); $redis->hSetNx('testhash','age',24);//如果已经存在,则不操作。若未设置,则赋值 //hMset可以直接设置数组,数组中如果存在已经定义的元素,会覆盖掉 $redis->hMset('testhash',array('sex'=>'男','home'=>'山东省武松打虎故乡','name'=>'twj2')); $redis->hSet('testhash','home','山东省'); echo $redis->zCount('testhash',0,3); //hMget可以根据多个键取对应的值,返回值是键值形式的数组 $hashnamesex = $redis->hMget('testhash',array('name','age')); print_r($hashnamesex); //根据特定单个键取值 $hashname = $redis->hGet('testhash','name'); echo $hashname."<br/>"; //取出所有的键 $hashallkey = $redis->hKeys('testhash'); print_r($hashallkey); //取出所有的值 $hashallvalue = $redis->hVals('testhash'); print_r($hashallvalue); //hgetall获取hash的键值 $hasharr = $redis->hgetall('testhash'); print_r($hasharr); echo "<hr/><br/>"; //$redis->delete('testhash');//删除testhash //与数据库关联 $mysql = new PDO("mysql:host=localhost;dbname=blog","root","",array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $rs = $mysql -> query("select title,content from tbl_post limit 10"); $i=0; while($row = $rs -> fetch()){ $rows[$i]['title']=$row['title']; $rows[$i]['content']=$row['content']; $i=$i+1; } $redisrows = json_encode($rows); $redis->setex('blog','300',$redisrows); $blog = $redis->get('blog'); print_r(json_decode($blog)); //$redis->delete('blog'); $redis->hSet('blog2','data',$redisrows); $blog2 = $redis->hGet('blog2','data'); print_r(json_decode($blog2)); $redis->expire('testhash',300); $redis->expire('blog2',300);//过期时间 //$redis->delete('blog2'); //$redis->delete('testhash'); //$redis->delete('blog2'); //zset与hash结合使用比较方便 $redis->zAdd('zset',0,'test0content'); $redis->zAdd('zset',1,'test1content'); $redis->zAdd('zset',3,'test3content'); $redis->zAdd('zset',2,'test2content'); $redis->zAdd('zset',4,'test4content');//zAdd(key,score,value)score只能为数字,当value相同时候,会覆盖掉score $zset = $redis->zRange('zset',0,2);//返回zadd数组 echo '<br/><hr/>zset'; print_r($zset); //$redis->delete('zset'); ?>对于那些用windows的wamp集成环境没有装上redis的也可以找我。
联系qq:274501366
代码很基础,转载请注明。
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- php查询mysql并缓存到redis
- php查询mysql并缓存到redis
- Redis安装及主从配置
- redis 3.0.1 在CentOS上的安装
- 深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存
- redis源码分析 dict字典的实现和内部应用
- 关于Redis两种持久化方式的说明
- nginx+lua+redis搭建
- HBase,Redis
- centOS上安装redis
- Java + Redis3.0.2集群的应用(四)——Aop完美整合
- 使用Python操作Redis
- Java + Redis3.0.2集群的应用(三)——数据设计
- Redis
- redis学习-事物,生存时间
- reids使用
- asp.net mvc 用Redis实现分布式集群共享Session。
- jedis访问redis学习笔记
- jedis访问redis学习笔记
- Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解