Utils-Redis工具类

34 篇文章 0 订阅
17 篇文章 0 订阅

Maven依赖:

<!-- redis -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 

package com.utils;

import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis封装工具类
 * By CHENYB Date 2019-05-22
 */
@Component
public class RedisUtil
{
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 断言键值对是否存在
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean exists(final String key)
    {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public Object get(final String key)
    {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value)
    {
        boolean result = false;
        try
        {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value, Long expireTime)
    {
        boolean result = false;
        try
        {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 字符串类型: 将key进行递增。如果key不存在,操作之前,key就会被置为0.
     * 如果key的value类型错误或者是个不能表示成数字的字符串,就返回错误。
     * 这个操作最多支持64位有符号的整形数字。
     * @param key
     * @param offset
     * @return
     */
    public Long incrby(final String key, final Long offset)
    {
        @SuppressWarnings("unchecked")
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        Object result = operations.get(key);
        Long count = Long.valueOf(result.toString());
        operations.set(key, count + 1);
        return count + 1;
    }

    /**
     * 缺席填充
     * @param redisKey
     * @param value
     * @param seconds
     * @return
     */
    public boolean setIfAbsent(String redisKey, Object value, long seconds)
    {
        boolean result = false;
        BoundValueOperations boundValueOperations = redisTemplate
                .boundValueOps(redisKey);
        result = boundValueOperations.setIfAbsent(value);
        //保存成功,更新时间,否则不更新过期时间
        if ((result && seconds != -1)||seconds == 0)
        {
            boundValueOperations.expire(seconds, TimeUnit.SECONDS);
        }
        return result;
    }

    /**
     * 删除缓存
     * @param key
     * @return
     */
    public boolean clearKey (final String key){
        return redisTemplate.delete( key );
    }

    /**
     * 删除缓存中所有数据
     * @return
     */
    public Long clearKeys (){
        return redisTemplate.delete( redisTemplate.keys( "*" ) );
    }

    /**
     * 获取结果集
     * @param key
     * @return
     */
    public BoundHashOperations hgetList(String key) {
        return redisTemplate.boundHashOps(key);
    }

}

测试代码:

package com.example.demo;

import com.utils.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTests{


    @Test
    public void test() {
        System.out.println("ok!");
    }

    @Resource
    private RedisUtil redisUtil;
    @Test
    public void existsTest() {
        boolean exists = redisUtil.exists("111");
        System.out.println(exists);
    }

    @Test
    public void insertTest() {
        boolean chenyb = redisUtil.set("111", "CHENYB");
        System.out.println(chenyb);
    }

    @Test
    public void getTest() {
        Object o = redisUtil.get("111");
        System.out.println(String.valueOf(o));
    }

 
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值