spring-data-redis

一、值类型

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")

public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue(){
    redisTemplate.boundValueOps("name").set("222");
}

//获取value
@Test
public void getValue(){
    System.out.println(redisTemplate.boundValueOps("name").get());
}

//删除
@Test
public void deleteValue(){
    redisTemplate.delete("name");
}
}

二、List类型

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")
public class TestList {
@Autowired
RedisTemplate redisTemplate;

/**
 * 右压栈:后添加对象排在后边
 */
@Test
public void setRightList(){
    redisTemplate.boundListOps("testlist").rightPush("第一");
    redisTemplate.boundListOps("testlist").rightPush("第二");
    redisTemplate.boundListOps("testlist").rightPush("第三");
}

/**
 * 左压栈:后添加对象排在后边
 */
@Test
public void setLeftList(){
    redisTemplate.boundListOps("testlist").leftPush("第一");
    redisTemplate.boundListOps("testlist").leftPush("第二");
    redisTemplate.boundListOps("testlist").leftPush("第三");
}

/**
 * 查询索引0到10元素
 */
@Test
public void getList(){
   List list=redisTemplate.boundListOps("testlist").range(0,10);
    System.out.println(list);
}

/**
 * 根据索引查询元素
 */
@Test
public void testSearchByIndex(){
    Object testlist = redisTemplate.boundListOps("testlist").index(1);
    System.out.println(testlist.toString());

}
/**
 * 移除集合某个元素
 */
@Test
public void testRemoveByIndex(){
    redisTemplate.boundListOps("testlist").remove(1,"第二");
}
}

三、Hash类型

   @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")

public class TestHash {
@Autowired
RedisTemplate redisTemplate;

@Test
public void testSetHash(){
    redisTemplate.boundHashOps("testhash").put("a","张三");
    redisTemplate.boundHashOps("testhash").put("b","李四");
    redisTemplate.boundHashOps("testhash").put("c","王五");
    redisTemplate.boundHashOps("testhash").put("d","赵六");
}

/**
 * 获取所有值
 */
@Test
public void testGetAllValue(){
    List testhash = redisTemplate.boundHashOps("testhash").values();
    System.out.println(testhash);
}

/**
 * 根据key获取值
 */
@Test
public void testGetValueByKey(){
    Object o = redisTemplate.boundHashOps("testhash").get("b");
    System.out.println(o);
}
}

四、Set类型

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")
public class TestSet {
@Autowired
RedisTemplate redisTemplate;

@Test
public void addSet(){
    redisTemplate.boundSetOps("nameset").add("曹操");
    redisTemplate.boundSetOps("nameset").add("刘备");
    redisTemplate.boundSetOps("nameset").add("孙权");
}

@Test
public void getSet(){
    Set nameset = redisTemplate.boundSetOps("nameset").members();
    System.out.println(nameset);

}

@Test
public void delSet(){
    redisTemplate.delete("nameset");
}
}

五、ZSet类型

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")
public class TestZSet {
@Autowired
RedisTemplate redisTemplate;
@Test
public void setValue(){
    redisTemplate.boundZSetOps("testzset").add("张三",1000);
    redisTemplate.boundZSetOps("testzset").add("李四",0);
    redisTemplate.boundZSetOps("testzset").add("王五",10);
}

/**
 * 查询,由低到高排序
 */
@Test
public void getValue(){
    Set testzset = redisTemplate.boundZSetOps("testzset").range(0, -1);
    System.out.println(testzset);
}

/**
 * 查询,由分数高到低的前10个
 */
@Test
public void getsqe(){
    Set testzset = redisTemplate.boundZSetOps("testzset").reverseRange(0, 9);
    System.out.println(testzset);
}

/**
 * 增加分数
 */
@Test
public void addScore(){
  redisTemplate.boundZSetOps("testzset").incrementScore("李四",10000);
}

/**
 * 查询值和分数
 * TypedTuple是值与分数的封装。
 */
@Test
public void getValueAndScore(){
    Set<ZSetOperations.TypedTuple> testzset = redisTemplate.boundZSetOps("testzset").reverseRangeByScoreWithScores(0,-1);
    for(ZSetOperations.TypedTuple typedTuple:testzset){
        System.out.println("姓名:"+typedTuple.getValue());
        System.out.println("分数:"+typedTuple.getScore());
    }
}

//删除
@Test
public void deleteValue(){
    redisTemplate.delete("testzset");
}
}

六、设置过期时间

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")
public class ExpireRedis {
@Autowired
RedisTemplate redisTemplate;

@Test
public void expireValue(){
    redisTemplate.boundValueOps("name").set("测试");
    redisTemplate.boundValueOps("name").expire(20, TimeUnit.SECONDS);
}
}

附加一 applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"      
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


 <context:property-placeholder location="classpath:redis-config.properties" />
   
   <!-- redis 相关配置 --> 
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
 <property name="maxIdle" value="${redis.maxIdle}" />   
 <property name="maxWaitMillis" value="${redis.maxWait}" />  

 </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>

</beans> 

附加二 redis-config.properties

redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
Last modification:January 9th, 2020 at 02:37 am

Leave a Comment