redis:spring-data-redis 你的名字 2022-07-14 01:11 164阅读 0赞 首先需要安装redis: 下载地址:[https://github.com/MSOpenTech/redis/releases][https_github.com_MSOpenTech_redis_releases] 完成后,开始写代码: 使用注解的方式进行配置 @Configuration @EnableWebMvc @ComponentScan(basePackages = { "xx.redis"}) @PropertySource({ "classpath:jedis.properties"}) public class ApplicationConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Autowired private Environment env; @Bean public Gson gson(){ return new Gson(); } @Bean public JedisPoolConfig poolConfig(){ Integer maxIdle = getIntValueFromEnv("jedis.maxIdle"); Integer maxTotal = getIntValueFromEnv("jedis.maxTotal"); Long maxWaitMillis = getLongValueFromEnv("jedis.maxWaitMillis"); Boolean testOnBorrow = getBooleanValueFromEnv("jedis.testOnBorrow"); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle == null ? JedisPoolConfig.DEFAULT_MAX_IDLE : maxIdle); jedisPoolConfig.setMaxTotal(maxTotal == null ? JedisPoolConfig.DEFAULT_MAX_TOTAL : maxTotal); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis == null ? JedisPoolConfig.DEFAULT_MAX_WAIT_MILLIS : maxWaitMillis); jedisPoolConfig.setTestOnBorrow(testOnBorrow == null ? JedisPoolConfig.DEFAULT_TEST_ON_BORROW : testOnBorrow); return jedisPoolConfig; } @Bean public JedisConnectionFactory connectionFactory(){ JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig()); String hostName = getStringValueFromEnv("jedis.host"); System.out.println("hostName:"+hostName); Integer port = getIntValueFromEnv("jedis.port"); String password = getStringValueFromEnv("jedis.password"); jedisConnectionFactory.setHostName(hostName); if(port != null) jedisConnectionFactory.setPort(port); if(port != null) jedisConnectionFactory.setPassword(password == null ? "" : password); return jedisConnectionFactory; } @Bean public StringRedisSerializer stringRedisSerializer(){ return new StringRedisSerializer(); } @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public RedisTemplate redisTemplate(){ RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory()); redisTemplate.setKeySerializer(stringRedisSerializer()); redisTemplate.setEnableTransactionSupport(true); return redisTemplate; } private String getStringValueFromEnv(String key) { return env.getProperty(key) == null ? null : String.valueOf(env.getProperty(key)); } private Long getLongValueFromEnv(String key) { return env.getProperty(key) == null ? null : Long.valueOf(env.getProperty(key)); } private Boolean getBooleanValueFromEnv(String key) { return env.getProperty(key) == null ? null : Boolean.valueOf(env.getProperty(key)); } public Integer getIntValueFromEnv(String key){ return env.getProperty(key) == null ? null : Integer.valueOf(env.getProperty(key)); } } jedis.properties: #jedis config jedis.testOnBorrow=true jedis.host = 192.168.81.58 Util: package xx.redis; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component("listOps") public class ListOps { @Autowired private RedisTemplate<String, String> redisTemplate; /** * 压栈 * * @param key * @param value * @return */ public Long push(String key, String value) { return redisTemplate.opsForList().leftPush(key, value); } /** * 出栈 * * @param key * @return */ public String pop(String key) { return redisTemplate.opsForList().leftPop(key); } /** * 入队 * * @param key * @param value * @return */ public Long in(String key, String value) { return redisTemplate.opsForList().rightPush(key, value); } /** * 出队 * * @param key * @return */ public String out(String key) { return redisTemplate.opsForList().leftPop(key); } /** * 栈/队列长 * * @param key * @return */ public Long length(String key) { return redisTemplate.opsForList().size(key); } /** * 范围检索 * * @param key * @param start * @param end * @return */ public List<String> range(String key, int start, int end) { return redisTemplate.opsForList().range(key, start, end); } /** * 移除 * * @param key * @param i * @param value */ public void remove(String key, long i, String value) { redisTemplate.opsForList().remove(key, i, value); } /** * 检索 * * @param key * @param index * @return */ public String index(String key, long index) { return redisTemplate.opsForList().index(key, index); } /** * 置值 * * @param key * @param index * @param value */ public void set(String key, long index, String value) { redisTemplate.opsForList().set(key, index, value); } /** * 裁剪 * * @param key * @param start * @param end */ public void trim(String key, long start, int end) { redisTemplate.opsForList().trim(key, start, end); } } package xx.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.google.gson.Gson; @Component("seriazileUtil") public class SeriazileUtil { @Autowired private Gson gson; public String seriazileAsString(Object object){ if(object == null){ return null; } return gson.toJson(object); } public <T> T deserializeAsObject(String json, Class<T> clazz){ if(json == null || clazz == null){ return null; } return gson.fromJson(json, clazz); } } 测试: @Override public List<Teacher> listTeachers() { String key = "allTeacherCache"; Long length = listOps.length(key); if(length != 0){ List<String> teacherStringS = listOps.range(key, 0, Integer.valueOf(length.toString())); return jsonsToTeachers(teacherStringS); } List<Teacher> teachers = getAllTeachers(); setTeachersCache(teachers, key); return teachers; } private void setTeachersCache(List<Teacher> teachers, String key) { teachers.stream().forEach(teacher->listOps.push(key, seriazileUtil.seriazileAsString(teacher))); } private List<Teacher> jsonsToTeachers(List<String> json){ return json.stream().map(teacherStr->{ return seriazileUtil.deserializeAsObject(teacherStr, Teacher.class); }).collect(Collectors.toList()); } [https_github.com_MSOpenTech_redis_releases]: https://github.com/MSOpenTech/redis/releases
还没有评论,来说两句吧...