准备工作
1、引入 Jedis
和 SpringDataRedis
依赖。
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
2、redis.properties
文件
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
3、spring-redis.xml
文件
<context:property-placeholder location="classpath*:properties/redis-config.properties" />
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:password="${redis.pass}"
p:pool-config-ref="poolConfig"
/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
maxIdle :最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到Jedis
实例均是可用的
基本使用
值类型
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-redis.xml"})
public class ValueTest{
@Autowired
private RedisTemplate<String,Object> redisTemplate;
/**
* 存入值
*/
@Test
public void setValue() {
redisTemplate.boundValueOps("name").set("懒猴子CG");
System.out.println("缓存存入成功!");
}
/**
* 获取值
*/
@Test
public void getValue() {
String name = (String) redisTemplate.boundValueOps("name").get();
System.out.println("缓存:" + name);
}
/**
* 删除值
*/
@Test
public void deleteValue() {
redisTemplate.delete("name");
System.out.println("缓存删除成功!");
}
}
Set类型
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-redis.xml"})
public class SetTest {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
/**
* 存入值
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("NBASet").add("欧文");
redisTemplate.boundSetOps("NBASet").add("哈登");
redisTemplate.boundSetOps("NBASet").add("乔治");
}
/**
* 获取值
*/
@Test
public void getValue(){
Set members = redisTemplate.boundSetOps("NBASet").members();
System.out.println(members);
}
/**
* 删除某值
*/
@Test
public void deleteValue(){
redisTemplate.boundSetOps("NBASet").remove("哈登");
}
/**
* 删除所有值
*/
@Test
public void deleteAllValue(){
redisTemplate.delete("NBASet");
}
}
List类型
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-redis.xml"})
public class ListTest {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
/**
* 右压栈:后添加的对象排在后边
*/
@Test
public void testSetRightValue(){
redisTemplate.boundListOps("NBAList").rightPush("欧文");
redisTemplate.boundListOps("NBAList").rightPush("哈登");
redisTemplate.boundListOps("NBAList").rightPush("乔治");
}
/**
* 显示右压栈集合
*/
@Test
public void testGetRightValue(){
List list = redisTemplate.boundListOps("NBAList").range(0, 10);
System.out.println(list);
}
/**
* 左压栈:后添加的对象排在前边
*/
@Test
public void testSetLeftValue(){
redisTemplate.boundListOps("NBAList").leftPush("欧文");
redisTemplate.boundListOps("NBAList").leftPush("哈登");
redisTemplate.boundListOps("NBAList").leftPush("乔治");
}
/**
* 显示左压栈集合
*/
@Test
public void testGetLeftValue(){
List list = redisTemplate.boundListOps("NBAList").range(0, 10);
System.out.println(list);
}
/**
* 查询集合某个元素
*/
@Test
public void testSearchByIndex(){
String s = (String) redisTemplate.boundListOps("NBAList").index(3);
System.out.println(s);
}
/**
* 移除集合某个元素
*/
@Test
public void testRemoveByIndex() {
redisTemplate.boundListOps("NBAList").remove(1, "哈登");
}
}
Hash类型
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-redis.xml"})
public class HashTest {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
/**
* 存入值
*/
@Test
public void testSetValue(){
redisTemplate.boundHashOps("NBAHash").put("OW", "欧文");
redisTemplate.boundHashOps("NBAHash").put("HD", "哈登");
redisTemplate.boundHashOps("NBAHash").put("QZ", "乔治");
}
/**
* 获取所有key
*/
@Test
public void testGetKeys(){
Set s = redisTemplate.boundHashOps("NBAHash").keys();
System.out.println(s);
}
/**
* 获取所有value
*/
@Test
public void testGetValues(){
List values = redisTemplate.boundHashOps("NBAHash").values();
System.out.println(values);
}
/**
* 根据key取值
*/
@Test
public void testGetValueByKey(){
String value = (String) redisTemplate.boundHashOps("NBAHash").get("OW");
System.out.println(value);
}
/**
* 根据key删值
*/
@Test
public void testRemoveValueByKey(){
redisTemplate.boundHashOps("NBAHash").delete("QZ");
}
}