项目中使用redis
使用jedis操作redis测试
进行测试之前需要引入依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- jackson相关依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
测试
public class RedisTest {
Jedis jedis = null;
// 1.建立连接
// 2.操作数据(五种)
@Before
public void init() {
// 打开连接
//创建jedis对象,如果不写端口,默认6379
jedis=new Jedis("127.0.0.1",6379);
//验证密码,如果无密码,这步可省略
//jedis.auth("");
}
@Test
public void testString(){
//向redis放string类型数据
//姓名
jedis.set("name", "xiaofang");
//年令
jedis.set("age", "18");
//年令加1
jedis.incr("age");
System.out.println(jedis.get("age"));
//年令加5
jedis.incrBy("age", 5);
System.out.println("加5岁之后"+jedis.get("age"));
//减1
jedis.decr("age");
System.out.println("减1岁之后"+jedis.get("age"));
jedis.decrBy("age", 6);
System.out.println("减6岁之后"+jedis.get("age"));
}
@Test
public void testHash(){
//可以向这里放map
Map<String,String> map=new HashMap<>();
map.put("name", "chunyu");
map.put("pass", "123456");
map.put("age", "23");
jedis.hmset("liuchunyu", map);
//也可以直接向key/value
jedis.hset("liuchunyu", "email", "liucy@neusoft.com");
//读取单个信息
System.out.println(jedis.hget("liuchunyu", "email"));
//读取所有key
Set<String> keys=jedis.hkeys("liuchunyu");
for(String key:keys){
System.out.println(key);
}
System.out.println("---------------------");
//读取所有values
List<String> vs=jedis.hvals("liuchunyu");
for(String v:vs){
System.out.println(v);
}
System.out.println("---------------------");
//读取所有的key/value
Map<String,String> items=jedis.hgetAll("liuchunyu");
Set<Entry<String,String>>all=items.entrySet();
for(Entry e:all){
System.out.println(e.getKey());
System.out.println(e.getValue());
}
//keyset方式学生练习一下
System.out.println("------------------->");
Set<String> mm=items.keySet();
for(String m:mm){
System.out.println(items.get(m));
}
}
@Test
public void testList(){
//向list里放数据
//从上面放6个数据
jedis.lpush("images", "1.jpg");
jedis.lpush("images", "2.jpg");
jedis.lpush("images", "3.jpg");
jedis.lpush("images", "4.jpg");
jedis.lpush("images", "5.jpg");
jedis.lpush("images", "6.jpg");
//截取数据,留下哪些,索引从0开始,最后一个为-1
jedis.ltrim("images", 0, 3);
//读取所有数据
List<String> list=jedis.lrange("images", 0, -1);
for(String item:list){
System.out.println(item);
}
}
@Test
public void testSet(){
//成员添加
jedis.sadd("users", "ms");
jedis.sadd("users", "cx");
jedis.sadd("users", "jhq");
jedis.sadd("users", "fyl");
Set<String> us=jedis.smembers("users");
//打印所有成员
for(String u:us){
System.out.println(u);
}
//获取成员人个数
System.out.println(jedis.scard("users"));
}
@Test
public void testSortedSet() {
// 构建map
Map<String, Double> m = new HashMap<>();
m.put("m10", 1.0);
m.put("m12", 1.2);
m.put("m11", 1.1);
m.put("m16", 1.6);
m.put("m15", 1.5);
m.put("m15", 1.7);
m.put("m13", 1.3);
m.put("m14", 1.4);
// 放入redis
jedis.zadd("movies", m);
// 读取
// 第一种正序读
Set<String> ms = jedis.zrange("movies", 0, -1);
for (String item : ms) {
System.out.println(item);
}
System.out.println("倒序读---------------->");
// 第二种倒序读
Set<String> ms_r = jedis.zrevrange("movies", 0, -1);
for (String item : ms_r) {
System.out.println(item);
}
//带序号读取--正序
System.out.println("正序带序号---------------->");
Set<String> ms_score=jedis.zrangeByScore("movies", 1.0, 1.7);
for(String sc:ms_score){
System.out.println(sc);
}
//带序号读取-倒序
System.out.println("倒序带序号---------------->");
Set<String> ms_rscore=jedis.zrevrangeByScore("movies", 1.7, 1.0);
for(String sc:ms_rscore){
System.out.println(sc);
}
}
}
## 存取对象 ##
@Test
public void test02() throws IOException{
System.out.println("初始化完成,准备存储对象...");
Position p=new Position();
p.setLon(123.456);
p.setLat(41.23);
ObjectMapper om=new ObjectMapper();
jedis.set("18642046589", om.writeValueAsString(p));
//设置session的过期时间 时间单位是秒
jedis.expire("18642046589", 30*60);
Position p1=new Position();
p1.setLon(124.456);
p1.setLat(42.23);
jedis.set("18642046589", om.writeValueAsString(p1));
String result=jedis.get("18642046589");
Position p2=om.readValue(result, Position.class);
System.out.println("转换回来后:"+p2.getLat());
}
public class Position implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5681977159464086780L;
private double lon;
private double lat;
public double getLon() {
}