Redis入门-windows下的安装与使用
Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store)。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list),集合(sets) 和 有序集合(sorted sets)等类型。
初次使用,先在windows下小试一把,根据网上的资料整理下,以作为日志记录。
1.windows下安装
官网下载的是linux版,windows版在github上,由Microsoft Open Tech group提供的:
https://github.com/MSOpenTech/redis
可以直接点右则Download Zip下载,也可以通过git克隆。
下载后解压到D盘根目录:D:\redis-2.8
bin目录下有个release文件,可以解压bin目录,也可以放到D:\redis-2.8
这里我省事,直接解压到D:\redis-2.8,环境变量懒得配了
2.启动服务端
启动服务:打开cmd,进到目录D:\redis-2.8,执行如下命令:
redis-server redis.windows.conf
执行后报如下错误:
D:\redis-2.8>redis-server.exe redis.windows.conf [7736] 19 Apr 21:36:42.974 # The Windows version of Redis allocates a large memory mapped file for sharing the heap with the forked process used in persistence operations. This file will be created in the current working directory or the directory specified by the 'dir' directive in the .conf file. Windows is reporting that there is insufficient disk space available for this file (Windows error 0x70). You may fix this problem by either reducing the size of the Redis heap with the --maxheap flag, or by starting redis from a working directory with sufficient space available for the Redis heap. Please see the documentation included with the binary distributions for more details on the --maxheap flag. Redis can not continue. Exiting.
根据提示,是 maxheap 标识有问题,打开配置文件 redis.windows.conf ,搜索 maxheap , 然后直接指定好内容即可。
....... # # maxheap <bytes> maxheap 1024000000 .......
然后再次启动,OK,成功。
D:\redis-2.8>redis-server redis.windows.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.8.17 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 5856 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [5856] 19 Apr 20:24:12.984 # Server started, Redis version 2.8.17 [5856] 19 Apr 20:24:12.984 * DB loaded from disk: 0.000 seconds [5856] 19 Apr 20:24:12.984 * The server is now ready to accept connections on po rt 6379
3.启动客户端测试
根据自带的客户端软件测试,双击打开 redis-cli.exe, 如果不报错,则连接上了本地服务器,然后测试,比如 set命令,get命令:
127.0.0.1:6379> set hello redis OK 127.0.0.1:6379> get hello "redis" 127.0.0.1:6379>
4.基于Java开发包(Jedis)测试
新建maven工程,添加Jedis依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.0.0</version> <type>jar</type> <scope>compile</scope> </dependency>
测试类:
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * Redis的官方首选Java开发包jedis */ public class JedisTest { JedisPool pool; Jedis jedis; public static void main(String[] args) { JedisTest jt = new JedisTest(); jt.setUp(); jt.testGet(); } public void setUp() { pool = new JedisPool(new JedisPoolConfig(), "192.168.9.74"); jedis = pool.getResource(); // jedis.auth("password"); } public void testGet(){ System.out.println(jedis.get("yuwl")); } /** * Redis存储初级的字符串 * CRUD */ public void testBasicString(){ //-----添加数据---------- jedis.set("name","minxr");//向key-->name中放入了value-->minxr System.out.println(jedis.get("name"));//执行结果:minxr //-----修改数据----------- //1、在原来基础上修改 jedis.append("name","jarorwar"); //很直观,类似map 将jarorwar append到已经有的value之后 System.out.println(jedis.get("name"));//执行结果:minxrjarorwar //2、直接覆盖原来的数据 jedis.set("name","闵晓荣"); System.out.println(jedis.get("name"));//执行结果:闵晓荣 //删除key对应的记录 jedis.del("name"); System.out.println(jedis.get("name"));//执行结果:null /** * mset相当于 * jedis.set("name","minxr"); * jedis.set("jarorwar","闵晓荣"); */ jedis.mset("name","minxr","jarorwar","闵晓荣"); System.out.println(jedis.mget("name","jarorwar")); } /** * jedis操作Map */ public void testMap(){ Map<String,String> user=new HashMap<String,String>(); user.put("name","minxr"); user.put("pwd","password"); jedis.hmset("user",user); //取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List //第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数 List<String> rsmap = jedis.hmget("user", "name"); System.out.println(rsmap); //删除map中的某个键值 // jedis.hdel("user","pwd"); System.out.println(jedis.hmget("user", "pwd")); //因为删除了,所以返回的是null System.out.println(jedis.hlen("user")); //返回key为user的键中存放的值的个数1 System.out.println(jedis.exists("user"));//是否存在key为user的记录 返回true System.out.println(jedis.hkeys("user"));//返回map对象中的所有key [pwd, name] System.out.println(jedis.hvals("user"));//返回map对象中的所有value [minxr, password] Iterator<String> iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key)); } } /** * jedis操作List */ public void testList(){ //开始前,先移除所有的内容 jedis.del("java framework"); System.out.println(jedis.lrange("java framework",0,-1)); //先向key java framework中存放三条数据 jedis.lpush("java framework","spring"); jedis.lpush("java framework","struts"); jedis.lpush("java framework","hibernate"); //再取出所有数据jedis.lrange是按范围取出, // 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 System.out.println(jedis.lrange("java framework",0,-1)); } /** * jedis操作Set */ public void testSet(){ //添加 jedis.sadd("sname","minxr"); jedis.sadd("sname","jarorwar"); jedis.sadd("sname","闵晓荣"); jedis.sadd("sanme","noname"); //移除noname jedis.srem("sname","noname"); System.out.println(jedis.smembers("sname"));//获取所有加入的value System.out.println(jedis.sismember("sname", "minxr"));//判断 minxr 是否是sname集合的元素 System.out.println(jedis.srandmember("sname")); System.out.println(jedis.scard("sname"));//返回集合的元素个数 } public void test() throws InterruptedException { //keys中传入的可以用通配符 System.out.println(jedis.keys("*")); //返回当前库中所有的key [sose, sanme, name, jarorwar, foo, sname, java framework, user, braand] System.out.println(jedis.keys("*name"));//返回的sname [sname, name] System.out.println(jedis.del("sanmdde"));//删除key为sanmdde的对象 删除成功返回1 删除失败(或者不存在)返回 0 System.out.println(jedis.ttl("sname"));//返回给定key的有效时间,如果是-1则表示永远有效 jedis.setex("timekey", 10, "min");//通过此方法,可以指定key的存活(有效时间) 时间为秒 Thread.sleep(5000);//睡眠5秒后,剩余时间将为<=5 System.out.println(jedis.ttl("timekey")); //输出结果为5 jedis.setex("timekey", 1, "min"); //设为1后,下面再看剩余时间就是1了 System.out.println(jedis.ttl("timekey")); //输出结果为1 System.out.println(jedis.exists("key"));//检查key是否存在 System.out.println(jedis.rename("timekey","time")); System.out.println(jedis.get("timekey"));//因为移除,返回为null System.out.println(jedis.get("time")); //因为将timekey 重命名为time 所以可以取得值 min //jedis 排序 //注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) jedis.del("a");//先清除数据,再加入数据进行测试 jedis.rpush("a", "1"); jedis.lpush("a","6"); jedis.lpush("a","3"); jedis.lpush("a","9"); System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1] System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //输入排序后结果 System.out.println(jedis.lrange("a",0,-1)); } }
Redis会定时 保存数据到硬盘上,服务端窗口:
[5856] 19 Apr 20:24:12.984 # Server started, Redis version 2.8.17 [5856] 19 Apr 20:24:12.984 * DB loaded from disk: 0.000 seconds [5856] 19 Apr 20:24:12.984 * The server is now ready to accept connections on po rt 6379 [5856] 19 Apr 21:47:49.294 * 1 changes in 900 seconds. Saving... [5856] 19 Apr 21:47:49.521 # fork operation complete [5856] 19 Apr 21:47:49.553 * Background saving terminated with success
参考资料:
http://blog.csdn.net/renfufei/article/details/38474435
http://my.oschina.net/lujianing/blog/204103
http://www.cnblogs.com/stephen-liu74/archive/2012/04/16/2370212.html
http://redis.cn/
推荐阅读