redis的使用
程序员文章站
2022-07-10 18:34:13
...
redis的配置
package com.baiwang.zeus.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Configuration
public class RedisCacheConfig extends CachingConfigurerSupport {
Logger logger = LoggerFactory.getLogger(RedisCacheConfig.class);
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private int database;
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(this.host);
factory.setPort(this.port);
factory.setPassword(this.password);
factory.setDatabase(database);
return factory;
}
@Bean
public StringRedisTemplate redisTrafficTemplate() {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setDefaultSerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//默认超时时间,单位秒
cacheManager.setDefaultExpiration(3000);
//根据缓存名称设置超时时间,0为不超时
Map<String,Long> expires = new ConcurrentHashMap<>();
cacheManager.setExpires(expires);
return cacheManager;
}
}
注入使用
@Autowired
@Qualifier("redisTrafficTemplate")//指定上面的bean注入
private RedisTemplate redisTemplate;
public String registerPermission(String key) {
String stringValue =redisTemplate.opsForValue().get(key); //根据key获取vaalue
return stringValue ;
}
redisTemplate.opsForValue()的使用
- set(K key, V value)
新增一个字符串类型的值,key是键,value是值。
redisTemplate.opsForValue().set("stringKey","stringValue");
- List item
get(Object key)
String stringValue = redisTemplate.opsForValue().get("key")
- append(K key, String value)
在原有的值基础上新增字符串到末尾。
redisTemplate.opsForValue().append("key", "appendValue");
String stringValueAppend = redisTemplate.opsForValue().get("key");
System.out.println("通过append(K key, String value)方法修改后的字符串:"+stringValueAppend);
就不一一列举了,如果有想了解的同学,可以看一下这位同学的博客,介绍的很详细:redisTemplate.opsForValue()的使用
上一篇: 导入excel表格工具类
下一篇: Java随机产生昵称