在java中使用redis

在java中使用redis很简单,只需要添加jedist.jar,通过它的api就可以了。而且,api和redis的语法几乎完全相同。以下简单的测试:

参考:http://www.runoob.com/redis/redis-java.html

  1 package com.test.redis;
  2 
  3 import org.junit.Test;
  4 import redis.clients.jedis.Jedis;
  5 
  6 import java.util.HashMap;
  7 import java.util.List;
  8 import java.util.Map;
  9 import java.util.Set;
 10 
 11 /**
 12  * 学习使用redis
 13  * Created by Administrator on 2016/2/19.
 14  */
 15 public class HelloRedis {
 16     private static final Jedis jedis = new Jedis("127.0.0.1",6379);
 17 
 18     public static void main(String[] args) {
 19         jedis.set("test","Hello Redis");
 20         String test = jedis.get("test");
 21         System.out.println(test);
 22         jedis.del("test");
 23     }
 24 
 25     /**
 26      * 一些操作
 27      */
 28     @Test
 29     public void testOption(){
 30         //查看redis是否运行
 31         System.out.println(jedis.ping());
 32         String s = jedis.flushDB();
 33         System.out.println("清空后:"+s);
 34 
 35         System.out.println(jedis.echo("foo"));
 36 
 37         jedis.set("foo","存储变量foo");
 38         Boolean is = jedis.exists("foo");
 39         System.out.println("foo是否存在:"+is);
 40 
 41         Set<String> keys = jedis.keys("*");
 42         System.out.println("数据库中所有的keys:"+keys);
 43 
 44         Set<String> keys1 = jedis.keys("f*");
 45         System.out.println("数据库中以 f 为前缀的keys"+keys1);
 46 
 47         String foo = jedis.type("foo");
 48         System.out.println("foo的类型:"+foo);
 49 
 50         jedis.del("foo");
 51     }
 52 
 53     /**
 54      * jedis存储字符串
 55      */
 56     @Test
 57     public void testString(){
 58         jedis.flushDB();
 59         //--添加数据--
 60         jedis.set("name","miao");
 61         System.out.println("取出name:"+jedis.get("name"));
 62 
 63         //取出元数据,并修改
 64         String name = jedis.getSet("name", "被取出后修改");
 65         System.out.println("取出:"+name+" 修改:"+jedis.get("name"));
 66         String name1 = jedis.getrange("name", 0,5 );
 67         System.out.println("获取value值并截取(中文容易乱码):"+name1);
 68         //--覆盖--
 69         jedis.set("name","覆盖了");
 70         System.out.println("取出重设置后的name:"+jedis.get("name"));
 71         //拼接
 72         jedis.append("name"," be stronger!");
 73         System.out.println("拼接后的name:"+jedis.get("name"));
 74 
 75         jedis.del("name");
 76         System.out.println("删除后的name:"+jedis.get("name"));
 77         //设置多个
 78         jedis.mset("name","miao","age","25","birth","1991-02-22");
 79         jedis.incr("age");//+1
 80         System.out.println(jedis.get("name")+"-"+jedis.get("age")+"-"+jedis.get("birth"));
 81         jedis.del("name","age","birth");
 82     }
 83 
 84     /**
 85      * redis操作map
 86      */
 87     @Test
 88     public void testMap(){
 89         jedis.flushDB();
 90         //--添加数据--
 91         Map<String,String> map = new HashMap<>();
 92         map.put("name","miao");
 93         map.put("age","35");
 94         map.put("birth","1991-02-33");
 95         jedis.hmset("user",map);
 96         //取出
 97         List<String> user = jedis.hmget("user","name","age","birth");
 98         System.out.println(user);
 99         Map<String, String> user1 = jedis.hgetAll("user");//取出user
100         System.out.println("user1="+user1);
101 //        jedis.del("user");
102 
103         //删除某个属性
104         jedis.hdel("user","birth");
105         System.out.println(jedis.hmget("user","name","age","birth"));
106         Long count = jedis.hlen("user");//user的属性的个数
107         System.out.println("属性个数:"+count);
108         Boolean is = jedis.exists("user");//是否存在
109         System.out.println("是否存在:"+is);
110         Set<String> ukeys = jedis.hkeys("user");//user的所有属性
111         System.out.println(ukeys);
112         List<String> uvals = jedis.hvals("user");//user的所有属性值
113         System.out.println(uvals);
114 
115         jedis.del("user");
116     }
117 
118     /**
119      * redis操作list
120      */
121     @Test
122     public void testList(){
123         jedis.flushDB();
124         //查看
125         List<String> users = jedis.lrange("users", 0, -1);
126         System.out.println("list:"+users);
127         //向左插入
128         jedis.lpush("users","miao");
129         jedis.lpush("users","rui");
130         jedis.lpush("users","feng");
131         //向右插入
132         jedis.rpush("users","ming");
133         jedis.rpush("users","hong");
134         jedis.rpush("users","xing");
135         System.out.println("(注意顺序)插入的结果:"+jedis.lrange("users",0,-1));
136         //长度
137         Long len = jedis.llen("users");
138         System.out.println("list长度:"+len);
139         //子串
140         System.out.println("取出索引0到1的数据:"+jedis.lrange("users",0,1));
141         //修改单个值
142         jedis.lset("users",0,"我变成第一个");
143         System.out.println("修改单个值后:"+jedis.lrange("users",0,-1));
144         //获取指定下标的值
145         String users1 = jedis.lindex("users", 0);
146         System.out.println("下标0为:"+users1);
147         //删除指定下标的值
148         Long lrem = jedis.lrem("users", 0,"我变成第一个");
149         System.out.println("删除下标0结果:"+lrem+"   |"+jedis.lrange("users",0,-1));
150         //删除区间之外的值
151 //        jedis.ltrim("users",1,2);
152         //出栈
153         String users2 = jedis.lpop("users");
154         System.out.println("左出栈:"+users2+"  |"+jedis.lrange("users",0,-1));
155         String users3 = jedis.rpop("users");
156         System.out.println("右出栈:"+users3+"  |"+jedis.lrange("users",0,-1));
157 
158         jedis.del("users");
159         System.out.println(jedis.lrange("users",0,-1));
160     }
161 
162     /**
163      * jedis set
164      */
165     @Test
166     public void testSet(){
167         //添加
168         jedis.sadd("user","miao");
169         jedis.sadd("user","rui");
170         jedis.sadd("user","feng");
171         jedis.sadd("user","ming");
172         jedis.sadd("user","hong");
173         jedis.sadd("user","xing");
174         //查看
175         Set<String> user = jedis.smembers("user");
176         System.out.println("set:   "+user);
177         //移除
178         jedis.srem("user","miao");
179         System.out.println("移除后: "+jedis.smembers("user"));
180         //判断是否存在
181         Boolean is = jedis.sismember("user", "rui");
182         System.out.println("rui是否存在: "+is);
183         //随机取出一个
184         String one = jedis.srandmember("user");
185         System.out.println(one);
186         System.out.println(jedis.srandmember("user",2));
187         //返回个数
188         Long count = jedis.scard("user");
189         System.out.println("user个数:"+count);
190 
191         jedis.del("user");
192 
193     }
194 
195     /**
196      * zset
197      * 有序的set
198      */
199     @Test
200     public void testZset(){
201         jedis.flushDB();
202         //添加
203         jedis.zadd("zkey",12.1,"十二");
204         jedis.zadd("zkey",11.1,"11");
205         jedis.zadd("zkey",21.1,"20");
206         jedis.zadd("zkey",41.1,"41");
207         jedis.zadd("zkey",4,"4");
208         //查看
209         Set<String> zkey = jedis.zrange("zkey", 0, -1);
210         System.out.println("可以看到是以score排序的:"+zkey);
211     }
212 
213     /**
214      * 排序
215      */
216     @Test
217     public void testSort(){
218         jedis.del("list");
219         jedis.rpush("list","1");
220         jedis.rpush("list","4");
221         jedis.rpush("list","2");
222         jedis.rpush("list","20");
223         jedis.rpush("list","5");
224         jedis.rpush("list","6");
225         jedis.rpush("list","67");
226         System.out.println(jedis.lrange("list",0,-1));
227         System.out.println("排序:"+jedis.sort("list"));
228         System.out.println("排序不影响原存储:"+jedis.lrange("list",0,-1));
229     }
230 
231 
232 
233 }

 

posted @ 2016-02-21 21:43  Ryan.Miao  阅读(7628)  评论(0编辑  收藏  举报