springboot配置redis教程

springboot配置redis教程

pom.xml

<!--  springboot整合 redis -->
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

在这里插入图片描述
host:连接地址(redis服务所在主机地址)
password:密码
port:端口
jedis:pool:
max-active: 8
#最大空闲
max-idle: 8
#最小空闲
min-idle: 0

以下是redis基础配置包括

/**
 * @author yub
 * @describe talk something
 * @date 2018/11/14 001414:44
 */
@Configuration
@EnableCaching
public class RedisConfig {
	/**
	 * 生成key的策略
	 * @return
	 */
	@Bean
	public KeyGenerator keyGenerator() {
		return new KeyGenerator() {
			@Override
			public Object generate(Object target, Method method, Object... params) {
				StringBuilder sb = new StringBuilder();
				sb.append(target.getClass().getName());
				sb.append(method.getName());
				for (Object obj : params) {
					sb.append(obj.toString());
				}
				return sb.toString();
			}
		};
	}

	/**
	 * 缓存管理
	 * @return
	 */
	@Bean
	public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
		RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory).build();
		return redisCacheManager;
	}

	/**
	 * @Description: 防止redis入库序列化乱码的问题
	 * @return     返回类型
	 * @date 2018/4/12 10:54
	 */
	@Bean
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());//key序列化
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));  //value序列化

		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
}

redis操作对象

/**
 * @author yub
 * @describe redis 操作
 * @date 2018/11/14 001415:01
 */
@Component
public class RedisService<HK, V> {

	// 在构造器中获取redisTemplate实例, key(not hashKey) 默认使用String类型
	private RedisTemplate<String, V> redisTemplate;
	// 在构造器中通过redisTemplate的工厂方法实例化操作对象
	private HashOperations<String, HK, V> hashOperations;
	private ListOperations<String, V> listOperations;
	private ZSetOperations<String, V> zSetOperations;
	private SetOperations<String, V> setOperations;
	private ValueOperations<String, V> valueOperations;

	// IDEA虽然报错,但是依然可以注入成功, 实例化操作对象后就可以直接调用方法操作Redis数据库
	@Autowired
	public RedisService(RedisTemplate<String, V> redisTemplate) {
		this.redisTemplate = redisTemplate;
		this.hashOperations = redisTemplate.opsForHash();
		this.listOperations = redisTemplate.opsForList();
		this.zSetOperations = redisTemplate.opsForZSet();
		this.setOperations = redisTemplate.opsForSet();
		this.valueOperations = redisTemplate.opsForValue();
	}


	public void hashPut(String key, HK hashKey, V value) {
		hashOperations.put(key, hashKey, value);
	}

	public Map<HK, V> hashFindAll(String key) {
		return hashOperations.entries(key);
	}

	public V hashGet(String key, HK hashKey) {
		return hashOperations.get(key, hashKey);
	}

	public void hashRemove(String key, HK hashKey) {
		hashOperations.delete(key, hashKey);
	}

	public Long listPush(String key, V value) {
		return listOperations.rightPush(key, value);
	}

	public Long listUnshift(String key, V value) {
		return listOperations.leftPush(key, value);
	}

	public List<V> listFindAll(String key) {
		if (!redisTemplate.hasKey(key)) {
			return null;
		}
		return listOperations.range(key, 0, listOperations.size(key));
	}

	public V listLPop(String key) {
		return listOperations.leftPop(key);
	}

	public void setValue(String key, V value) {
		valueOperations.set(key, value);
	}
	//设置键值对信息及过期时间
	public void setValue(String key, V value, long timeout) {
		ValueOperations<String, V> vo = redisTemplate.opsForValue();
		vo.set(key, value, timeout, TimeUnit.MILLISECONDS);
	}


	public V getValue(String key) {
		return valueOperations.get(key);
	}

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

	public boolean expire(String key, long timeout, TimeUnit timeUnit) {
		return redisTemplate.expire(key, timeout, timeUnit);
	}
}

使用操作

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43994765/article/details/87624517
今日推荐