Java操作redis代码总结

jedis概念

讲到jedis,先来看看redis, redis是当今基本所有互联网产品都在使用的一种提供键值对形式的内存数据库。之所以说是内存数据库,是因为redis基于内存的读取和写入相比传统的数据库基于磁盘IO快上数倍。于是乎redis在现在的应用中使用的非常广泛。主要的作用在于:

1、提供缓存服务,存储访问频率高的热数据防止穿透到数据库
2、在分布式系统中可以作为实现分布式锁的一种实现方案

那jedis就是集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理。一般不直接使用jedis,而是在其上在封装一层,作为业务的使用。如果用spring的话,可以看看spring 封装的 redis Spring Data Redis

Spring-Data-Redis介绍

Spring-Data-Redis项目(简称SDR)对Redis的Key-Value数据存储操作提供了更高层次的抽象,类似于Spring Framework对JDBC支持一样。

项目主页: http://projects.spring.io/spring-data-redis/

项目文档: http://docs.spring.io/spring-data/redis/docs/1.5.0.RELEASE/reference/html/

Spring Data Redis 操作redis

操作redis list

  • 源代码

从redis list的末端开始遍历元素,取元素消费,然后删除,已测试,不会有问题
反方向遍历的话,redisTemplate.opsForList().index()函数得到的结果会有问题【index会变化】

  • redis list 可视化数据结构
    这里写图片描述
import org.springframework.data.redis.core.StringRedisTemplate;


@Autowired
private StringRedisTemplate redisTemplate;


@Scheduled(cron = "0 0/1 * * * ?")
public void latticePointPush() {
    System.out.println("start ------------------------------");

    // rightPush: Insert all the specified values at the tail of the list
    // stored at key.
    // 从list的尾部添加元素
    redisTemplate.opsForList().rightPush(KEY_OF_PUSH_FAIL_TIME, DateFormat.format(new Date()));
    redisTemplate.opsForList().rightPush(KEY_OF_PUSH_FAIL_TIME,
            DateFormat.format(new Date(new Date().getTime() + 1000 * 60)));
    redisTemplate.opsForList().rightPush(KEY_OF_PUSH_FAIL_TIME,
            DateFormat.format(new Date(new Date().getTime() + 1000 * 120)));
    redisTemplate.opsForList().rightPush(KEY_OF_PUSH_FAIL_TIME,
            DateFormat.format(new Date(new Date().getTime() + 1000 * 180)));

    Long size = Long.valueOf(redisTemplate.opsForList().size(KEY_OF_PUSH_FAIL_TIME));
    System.out.println("redis list size " + size);
    // 必要时需要对size进行条件判断
    // 从list的尾部开始遍历
    for (long i = size - 1; i >= 0; i--) {
        // Returns the element at index index in the list stored at key.
        // The index is zero-based, so 0 means the first element, 1 the
        // second element and so on.
        String value = redisTemplate.opsForList().index(KEY_OF_PUSH_FAIL_TIME, i);

        System.out.println(
                String.format("Returns the element at index index【%s】 in the list stored at key【%s】  is 【%s】", i,
                        KEY_OF_PUSH_FAIL_TIME, value));
        // rightPop: Removes and returns the last element of the list stored
        // at key.

        System.out.println("删除前,redis list size " + redisTemplate.opsForList().size(KEY_OF_PUSH_FAIL_TIME));

        // 删除并返回list的最后一个元素
        String delValue = redisTemplate.opsForList().rightPop(KEY_OF_PUSH_FAIL_TIME);

        System.out.println("已删除: " + delValue);

        System.out.println("删除后,redis list size " + redisTemplate.opsForList().size(KEY_OF_PUSH_FAIL_TIME));
    }

    System.out.println("finish ----------------------------");

}
  • 运行结果
start ------------------------------
redis list size 4
Returns the element at index index【3in the list stored at key【WA_LNMP_PUSH_TIME】  is2018-06-16 14:43:00】
删除前,redis list size 4
已删除: 2018-06-16 14:43:00
删除后,redis list size 3
Returns the element at index index【2in the list stored at key【WA_LNMP_PUSH_TIME】  is2018-06-16 14:42:00】
删除前,redis list size 3
已删除: 2018-06-16 14:42:00
删除后,redis list size 2
Returns the element at index index【1in the list stored at key【WA_LNMP_PUSH_TIME】  is2018-06-16 14:41:00】
删除前,redis list size 2
已删除: 2018-06-16 14:41:00
删除后,redis list size 1
Returns the element at index index【0in the list stored at key【WA_LNMP_PUSH_TIME】  is2018-06-16 14:40:00】
删除前,redis list size 1
已删除: 2018-06-16 14:40:00
删除后,redis list size 0
finish ----------------------------

猜你喜欢

转载自blog.csdn.net/thebigdipperbdx/article/details/80534941
今日推荐