springboot中redis实例

今天给大家分享一下redis在springboot中具体使用实例。

直接上代码吧!

引入redis 需要的jar

 <dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
</dependency>
@Configuration
@Component
public class RedisComponent {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public RedisComponent() {
    }

    @Bean
    @Primary
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        this.redisTemplate.setConnectionFactory(this.jedisConnectionFactory());
        this.redisTemplate.setValueSerializer(new StringRedisSerializer());
        return this.redisTemplate;
    }

    public void remove(String... keys) {
        String[] var2 = keys;
        int var3 = keys.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String key = var2[var4];
            this.remove(key);
        }

    }

    public void removePattern(String pattern) {
        Set<Serializable> keys = this.redisTemplate.keys(pattern);
        if (keys.size() > 0) {
            this.redisTemplate.delete(keys);
        }

    }

    public void remove(String key) {
        if (this.exists(key)) {
            this.redisTemplate.delete(key);
        }

    }

    public boolean exists(String key) {
        return this.redisTemplate.hasKey(key);
    }

    public Object get(String key) {
        ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
        Object result = operations.get(key);
        return result;
    }

    public boolean set(String key, Object value) {
        boolean result = false;

        try {
            ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return result;
    }

    public boolean set(String key, Object value, Long expireTime) {
        boolean result = false;

        try {
            ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
            operations.set(key, value);
            this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return result;
    }

    public boolean checkKey() {
        return false;
    }

    public boolean setBit(String key, long offset, boolean value, Long expireTime) {
        boolean result = false;

        try {
            ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
            operations.setBit(key, offset, value);
            this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return result;
    }

    public boolean getBit(String key, long offset) {
        ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
        return operations.getBit(key, offset);
    }

    public Long bitcount(final String key) {
        ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
        this.redisTemplate.boundValueOps(key);
        Object count = this.redisTemplate.execute(new RedisCallback<Object>() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.bitCount(key.getBytes());
            }
        });
        return StringUtils.isEmply(count) ? 0L : Long.valueOf(count + "");
    }

    public boolean convertAndSend(String key, String value) {
        boolean result = false;

        try {
            this.redisTemplate.convertAndSend(key, value);
            result = true;
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return result;
    }
}

上面这段这段代码是对操作redis的一些封装。

具体使用的过程,我们在controller中引入

@Autowired
private RedisComponent redisComponent;
redisComponent.set(imgCode,verifyCode,(long) 3600);

然后get取出想用的值就好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值