Java中Redis的简单使用

简单搭建的java中支持的Jedis操作Redis,链接:https://blog.csdn.net/qq_40437152/article/details/84771050

使用redisTemplate操作Redis数据库

pom文件  添加Spring封装的spring-boot-starter-data-redis

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

 <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.9.1</version>
 </dependency>

application.properties配置文件:

  redis:
    host: 47.92.162.82
    password: root
    port: 6379
    timeout: 2000
    pool:
      #最大连接数(负数表示没有限制)
      max-active: 1000
      #最大空闲连接
      max-idle: 10
      #最大阻塞等待时间(负数表示没有限制)
      max-wait: 100000
    database: 0

RedisConfig.java

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
	@Bean
	@ConditionalOnMissingBean(name="redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<Object, Object> template = new RedisTemplate<Object,Object>();
	     template.setConnectionFactory(redisConnectionFactory);
	     //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
	     
	     GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
	     
	     template.setValueSerializer(serializer);
	     //使用StringRedisSerializer来序列化和反序列化redis的key值
	     template.setKeySerializer(new StringRedisSerializer());
		
		return template;
	}
}

RedisUtils工具类

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {
	@Autowired		                                
    private RedisTemplate<Object, Object> template;

	
    public void put(Object key,Object value){
        ValueOperations<Object, Object> ops = template.opsForValue();
        ops.set(key,value);
    }
 
    public  Object get(Object key){
        ValueOperations<Object, Object> ops = template.opsForValue();
        return ops.get(key);
    }
    
    public void put(Object key,Object value,long timeout,TimeUnit unit){
        ValueOperations<Object, Object> ops = template.opsForValue();
        ops.set(key,value,timeout,unit);
    }
    
    public void put(Object key,Object value,long seconds){
        ValueOperations<Object, Object> ops = template.opsForValue();
        this.put(key, value, seconds, TimeUnit.SECONDS);
    }
    
    /**
	 * 删除对应的value
	 * 
	 * @param key
	 */
	public void remove(final Object key) {
		if (exists(key)) {
			template.delete(key);
		}
	}


	/**
	 * 返回redis中key过期时间 key已过期返回-1
	 */
	public Long getExpire(Object key){
		return template.getExpire(key);
	}

	/**
	 * 根据当前key和传入时间显示过期时间 key已过期返回-1
	 * @param key
	 * @param unit
	 * @return
	 */
	public long getExpire(Object key,TimeUnit unit){
		return template.getExpire(key,unit);
	}

	/**
	 * 返回随机key
	 * @return
	 */
	public Object RANDOMKEY(){
		return template.randomKey();
	}

	/**
	 * 判断缓存中是否有对应的key;value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final Object key) {
		return template.hasKey(key);
	}

 上面配置的RedisTemplate使用StringRedisSerializer来序列化和反序列化redis的key值,这一步还是很有必要的,在存储对象,或者redis判断是否存在key,如果没配置,会有问题(具体的就不深究了)。

现在,项目还在开发阶段,昨天有注意到一个问题,代码中有对key设置过期时间,但是该key过期后,并没有自动删除。

昨天,查资料看了很久,有注意到redis的三个删除策略。。这里也不说了,不是很了解,也没有说配置什么的。

不知道是否有什么大牛遇到过同样的问题,欢迎留言!!!

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值