文章目录

Spring Data Redis简介

redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写。企业开发通常采用Redis来实现缓存。同类的产品还有memcache 、memcached 、MongoDB等。

Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis。

​Spring-data-redis​​​是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对​​reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装​​​,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:
1.​​​连接池自动管理​​​,提供了一个高度封装的“​​RedisTemplate​​​”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
​​​ValueOperations​​​:简单K-V操作
​​​SetOperations​​​:set类型数据操作
​​​ZSetOperations​​​:zset类型数据操作
​​​HashOperations​​​:针对map类型的数据操作
​​​ListOperations​​:针对list类型的数据操作

准备工作:

建maven项目

结构如下:

Spring Data Redis引入和常用类型操作demo_spring

操作

值类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue(){
redisTemplate.boundValueOps("name").set("itcast");
}
@Test
public void getValue(){
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
@Test
public void deleteValue(){
redisTemplate.delete("name");;
}
}

Set类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestSet {

@Autowired
private RedisTemplate redisTemplate;

/**
* 存入值
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("nameset").add("曹操");
redisTemplate.boundSetOps("nameset").add("刘备");
redisTemplate.boundSetOps("nameset").add("孙权");
}

/**
* 提取值
*/
@Test
public void getValue(){
Set members = redisTemplate.boundSetOps("nameset").members();
System.out.println(members);
}

/**
* 删除集合中的某一个值
*/
@Test
public void deleteValue(){
redisTemplate.boundSetOps("nameset").remove("孙权");
}

/**
* 删除整个集合
*/
@Test
public void deleteAllValue(){
redisTemplate.delete("nameset");
}
}

List类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring/applicationContext-redis.xml")
public class TestList {

@Autowired
private RedisTemplate redisTemplate;

/**
* 右压栈: 后添加的元素排在后边
*/
@Test
public void testSetValue1() {
redisTemplate.boundListOps("namelist1").rightPush("刘备");
redisTemplate.boundListOps("namelist1").rightPush("关羽");
redisTemplate.boundListOps("namelist1").rightPush("张飞");
}

/**
* 显示右压栈的值
*/
@Test
public void testGetValue1() {
List list = redisTemplate.boundListOps("namelist1").range(0, 10);
System.out.println(list);
}

/**
* 左压栈
*/
@Test
public void testSetValue2() {
redisTemplate.boundListOps("namelist2").leftPush("刘备");
redisTemplate.boundListOps("namelist2").leftPush("关羽");
redisTemplate.boundListOps("namelist2").leftPush("张飞");
}

/**
* 显示左压栈的值
*/
@Test
public void testGetValue2() {
List list = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list);
}

/**
* 查询集合某个元素
*/
@Test
public void testSearchByIndex(){
String s = (String) redisTemplate.boundListOps("namelist2").index(2);
System.out.println(s);
}

/**
* 移除集合某个元素
*/
@Test
public void testRemoveByIndex(){
redisTemplate.boundListOps("namelist1").remove(2, "张飞");
}

/**
* 删除集合
*/
@Test
public void deleteValue() {
redisTemplate.delete("namelist1");
}
}

Hash类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring/applicationContext-redis.xml")
public class TestHash {

@Autowired
private RedisTemplate redisTemplate;

/**
* 存入值
*/
@Test
public void testSetValue(){
redisTemplate.boundHashOps("namehash").put("a", "唐僧");
redisTemplate.boundHashOps("namehash").put("b", "悟空");
redisTemplate.boundHashOps("namehash").put("c", "八戒");
redisTemplate.boundHashOps("namehash").put("d", "沙僧");
}

/**
* 提取所有的KEY
*/
@Test
public void testGetKeys(){
Set s = redisTemplate.boundHashOps("namehash").keys();
System.out.println(s);
}

/**
* 提取所有的值
*/
@Test
public void testGetValues(){
List values = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);
}


/**
* 根据KEY提取值
*/
@Test
public void testGetValueByKey(){
Object object = redisTemplate.boundHashOps("namehash").get("b");
System.out.println(object);
}


/**
* 根据KEY移除值
*/
@Test
public void testRemoveValueByKey(){
redisTemplate.boundHashOps("namehash").delete("c");
}

}

git地址

​https://github.com/hufanglei/pinyou/tree/spring-data-redis-demo​

项目中使用常用代码

下面是一个使用hash类型的代码片段逻辑,一般在赠删改的时候对redis进行修改。查询的时候先从redis中查询。大key为content,小key为分类id。

Spring Data Redis引入和常用类型操作demo_redis_02

Spring Data Redis引入和常用类型操作demo_redis_03

Spring Data Redis引入和常用类型操作demo_压栈_04