Redis入门-windows下的安装与使用

        Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store)。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list),集合(sets) 和 有序集合(sorted sets)等类型。

 

初次使用,先在windows下小试一把,根据网上的资料整理下,以作为日志记录。

 

1.windows下安装

官网:http://redis.io/

官网下载的是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,执行如下命令:

Java代码   收藏代码
  1. redis-server  redis.windows.conf  

 执行后报如下错误:

Java代码   收藏代码
  1. D:\redis-2.8>redis-server.exe redis.windows.conf  
  2. [773619 Apr 21:36:42.974 #  
  3. The Windows version of Redis allocates a large memory mapped file for sharing  
  4. the heap with the forked process used in persistence operations. This file  
  5. will be created in the current working directory or the directory specified by  
  6. the 'dir' directive in the .conf file. Windows is reporting that there is  
  7. insufficient disk space available for this file (Windows error 0x70).  
  8.   
  9. You may fix this problem by either reducing the size of the Redis heap with  
  10. the --maxheap flag, or by starting redis from a working directory with  
  11. sufficient space available for the Redis heap.  
  12.   
  13. Please see the documentation included with the binary distributions for more  
  14. details on the --maxheap flag.  
  15.   
  16. Redis can not continue. Exiting.  

 根据提示,是 maxheap 标识有问题,打开配置文件 redis.windows.conf ,搜索 maxheap , 然后直接指定好内容即可。

Java代码   收藏代码
  1. .......  
  2. #    
  3. # maxheap <bytes>  
  4. maxheap 1024000000  
  5. .......  

 然后再次启动,OK,成功。

Java代码   收藏代码
  1. D:\redis-2.8>redis-server redis.windows.conf  
  2.                 _._  
  3.            _.-``__ ''-._  
  4.       _.-``    `.  `_.  ''-._           Redis 2.8.17 (00000000/064 bit  
  5.   .-`` .-```.  ```\/    _.,_ ''-._  
  6.  (    '      ,       .-`  | `,    )     Running in stand alone mode  
  7.  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379  
  8.  |    `-._   `._    /     _.-'    |     PID: 5856  
  9.   `-._    `-._  `-./  _.-'    _.-'  
  10.  |`-._`-._    `-.__.-'    _.-'_.-'|  
  11.  |    `-._`-._        _.-'_.-'    |           http://redis.io  
  12.   `-._    `-._`-.__.-'_.-'    _.-'  
  13.  |`-._`-._    `-.__.-'    _.-'_.-'|  
  14.  |    `-._`-._        _.-'_.-'    |  
  15.   `-._    `-._`-.__.-'_.-'    _.-'  
  16.       `-._    `-.__.-'    _.-'  
  17.           `-._        _.-'  
  18.               `-.__.-'  
  19.   
  20. [585619 Apr 20:24:12.984 # Server started, Redis version 2.8.17  
  21. [585619 Apr 20:24:12.984 * DB loaded from disk: 0.000 seconds  
  22. [585619 Apr 20:24:12.984 * The server is now ready to accept connections on po  
  23. rt 6379  

 3.启动客户端测试

根据自带的客户端软件测试,双击打开 redis-cli.exe, 如果不报错,则连接上了本地服务器,然后测试,比如 set命令,get命令:

Java代码   收藏代码
  1. 127.0.0.1:6379> set hello redis  
  2. OK  
  3. 127.0.0.1:6379> get hello  
  4. "redis"  
  5. 127.0.0.1:6379>  

 4.基于Java开发包(Jedis)测试

新建maven工程,添加Jedis依赖:

Java代码   收藏代码
  1. <dependency>  
  2.             <groupId>redis.clients</groupId>  
  3.             <artifactId>jedis</artifactId>  
  4.             <version>2.0.0</version>  
  5.             <type>jar</type>  
  6.             <scope>compile</scope>  
  7.         </dependency>  

 测试类:

Java代码   收藏代码
  1. import redis.clients.jedis.Jedis;  
  2. import redis.clients.jedis.JedisPool;  
  3. import redis.clients.jedis.JedisPoolConfig;  
  4. import java.util.HashMap;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. /** 
  9.  * Redis的官方首选Java开发包jedis 
  10.  */  
  11. public class JedisTest {  
  12.    
  13.         JedisPool pool;  
  14.         Jedis jedis;  
  15.           
  16.         public static void main(String[] args) {  
  17.             JedisTest jt = new JedisTest();  
  18.             jt.setUp();  
  19.             jt.testGet();  
  20.         }  
  21.           
  22.         public void setUp() {  
  23.             pool = new JedisPool(new JedisPoolConfig(), "192.168.9.74");  
  24.    
  25.             jedis = pool.getResource();  
  26.          //   jedis.auth("password");  
  27.         }  
  28.    
  29.    
  30.         public void testGet(){  
  31.             System.out.println(jedis.get("yuwl"));  
  32.         }  
  33.    
  34.         /** 
  35.          * Redis存储初级的字符串 
  36.          * CRUD 
  37.          */  
  38.         public void testBasicString(){  
  39.             //-----添加数据----------  
  40.             jedis.set("name","minxr");//向key-->name中放入了value-->minxr  
  41.             System.out.println(jedis.get("name"));//执行结果:minxr  
  42.    
  43.             //-----修改数据-----------  
  44.             //1、在原来基础上修改  
  45.             jedis.append("name","jarorwar");   //很直观,类似map 将jarorwar append到已经有的value之后  
  46.             System.out.println(jedis.get("name"));//执行结果:minxrjarorwar  
  47.    
  48.             //2、直接覆盖原来的数据  
  49.             jedis.set("name","闵晓荣");  
  50.             System.out.println(jedis.get("name"));//执行结果:闵晓荣  
  51.    
  52.             //删除key对应的记录  
  53.             jedis.del("name");  
  54.             System.out.println(jedis.get("name"));//执行结果:null  
  55.    
  56.             /** 
  57.              * mset相当于 
  58.              * jedis.set("name","minxr"); 
  59.              * jedis.set("jarorwar","闵晓荣"); 
  60.              */  
  61.             jedis.mset("name","minxr","jarorwar","闵晓荣");  
  62.             System.out.println(jedis.mget("name","jarorwar"));  
  63.    
  64.         }  
  65.    
  66.         /** 
  67.          * jedis操作Map 
  68.          */  
  69.         public void testMap(){  
  70.             Map<String,String> user=new HashMap<String,String>();  
  71.             user.put("name","minxr");  
  72.             user.put("pwd","password");  
  73.             jedis.hmset("user",user);  
  74.             //取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List  
  75.             //第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数  
  76.             List<String> rsmap = jedis.hmget("user""name");  
  77.             System.out.println(rsmap);  
  78.    
  79.             //删除map中的某个键值  
  80. //        jedis.hdel("user","pwd");  
  81.             System.out.println(jedis.hmget("user""pwd")); //因为删除了,所以返回的是null  
  82.             System.out.println(jedis.hlen("user")); //返回key为user的键中存放的值的个数1  
  83.             System.out.println(jedis.exists("user"));//是否存在key为user的记录 返回true  
  84.             System.out.println(jedis.hkeys("user"));//返回map对象中的所有key  [pwd, name]  
  85.             System.out.println(jedis.hvals("user"));//返回map对象中的所有value  [minxr, password]  
  86.    
  87.             Iterator<String> iter=jedis.hkeys("user").iterator();  
  88.             while (iter.hasNext()){  
  89.                 String key = iter.next();  
  90.                 System.out.println(key+":"+jedis.hmget("user",key));  
  91.             }  
  92.    
  93.         }  
  94.    
  95.         /** 
  96.          * jedis操作List 
  97.          */  
  98.         public void testList(){  
  99.             //开始前,先移除所有的内容  
  100.             jedis.del("java framework");  
  101.             System.out.println(jedis.lrange("java framework",0,-1));  
  102.             //先向key java framework中存放三条数据  
  103.             jedis.lpush("java framework","spring");  
  104.             jedis.lpush("java framework","struts");  
  105.             jedis.lpush("java framework","hibernate");  
  106.             //再取出所有数据jedis.lrange是按范围取出,  
  107.             // 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有  
  108.             System.out.println(jedis.lrange("java framework",0,-1));  
  109.         }  
  110.    
  111.         /** 
  112.          * jedis操作Set 
  113.          */  
  114.         public void testSet(){  
  115.             //添加  
  116.             jedis.sadd("sname","minxr");  
  117.             jedis.sadd("sname","jarorwar");  
  118.             jedis.sadd("sname","闵晓荣");  
  119.             jedis.sadd("sanme","noname");  
  120.             //移除noname  
  121.             jedis.srem("sname","noname");  
  122.             System.out.println(jedis.smembers("sname"));//获取所有加入的value  
  123.             System.out.println(jedis.sismember("sname""minxr"));//判断 minxr 是否是sname集合的元素  
  124.             System.out.println(jedis.srandmember("sname"));  
  125.             System.out.println(jedis.scard("sname"));//返回集合的元素个数  
  126.         }  
  127.    
  128.         public void test() throws InterruptedException {  
  129.             //keys中传入的可以用通配符  
  130.             System.out.println(jedis.keys("*")); //返回当前库中所有的key  [sose, sanme, name, jarorwar, foo, sname, java framework, user, braand]  
  131.             System.out.println(jedis.keys("*name"));//返回的sname   [sname, name]  
  132.             System.out.println(jedis.del("sanmdde"));//删除key为sanmdde的对象  删除成功返回1 删除失败(或者不存在)返回 0  
  133.             System.out.println(jedis.ttl("sname"));//返回给定key的有效时间,如果是-1则表示永远有效  
  134.             jedis.setex("timekey"10"min");//通过此方法,可以指定key的存活(有效时间) 时间为秒  
  135.             Thread.sleep(5000);//睡眠5秒后,剩余时间将为<=5  
  136.             System.out.println(jedis.ttl("timekey"));   //输出结果为5  
  137.             jedis.setex("timekey"1"min");        //设为1后,下面再看剩余时间就是1了  
  138.             System.out.println(jedis.ttl("timekey"));  //输出结果为1  
  139.             System.out.println(jedis.exists("key"));//检查key是否存在  
  140.             System.out.println(jedis.rename("timekey","time"));  
  141.             System.out.println(jedis.get("timekey"));//因为移除,返回为null  
  142.             System.out.println(jedis.get("time")); //因为将timekey 重命名为time 所以可以取得值 min  
  143.    
  144.             //jedis 排序  
  145.             //注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的)  
  146.             jedis.del("a");//先清除数据,再加入数据进行测试  
  147.             jedis.rpush("a""1");  
  148.             jedis.lpush("a","6");  
  149.             jedis.lpush("a","3");  
  150.             jedis.lpush("a","9");  
  151.             System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1]  
  152.             System.out.println(jedis.sort("a")); //[1, 3, 6, 9]  //输入排序后结果  
  153.             System.out.println(jedis.lrange("a",0,-1));  
  154.    
  155.         }  
  156. }  

 5.Redis会定时 保存数据到硬盘上,服务端窗口:

Java代码   收藏代码
  1. [585619 Apr 20:24:12.984 # Server started, Redis version 2.8.17  
  2. [585619 Apr 20:24:12.984 * DB loaded from disk: 0.000 seconds  
  3. [585619 Apr 20:24:12.984 * The server is now ready to accept connections on po  
  4. rt 6379  
  5. [585619 Apr 21:47:49.294 * 1 changes in 900 seconds. Saving...  
  6. [585619 Apr 21:47:49.521 # fork operation complete  
  7. [585619 Apr 21:47:49.553 * Background saving terminated with success  

 6.参考资料:

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/

猜你喜欢

转载自yuwenlin.iteye.com/blog/2204196
今日推荐