标签:服务 public col vat ota mamicode nts img 文件
导入pom文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
1、redis 连接 端口配置
2、使用的是redistemplate 加载redis,写了个加载配置类并且 让DefaultCacheService注入了缓存服务
package com.fyun.tewebcore.config; import com.fyun.common.utils.CacheService; import com.fyun.common.utils.impl.DefaultCacheService; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; /** * @author zhourui * @create 2020/1/7 */ @Configuration @PropertySource(value = {"classpath:spring/redis-${spring.profiles.active}.properties"}) public class RedisConfig { @Value("${redis.database}") private Integer database; @Value("${redis.host}") private String host; @Value("${redis.port}") private Integer port; @Value("${redis.timeout}") private Integer timeout; //注入刚才的cacheservice @Bean public CacheService cacheService() { //注入spring return new DefaultCacheService(getredisTemplate()); } @Bean public RedisTemplate getredisTemplate(){ RedisTemplate redisTemplate = new RedisTemplate(); //redis设置工厂配置 redisTemplate.setConnectionFactory(this.redisConnection()); return redisTemplate; } @Bean public RedisConnectionFactory redisConnection(){ JedisConnectionFactory jedisFactory = new JedisConnectionFactory(); jedisFactory.setDatabase(database); jedisFactory.setHostName(host); jedisFactory.setPort(port); jedisFactory.setTimeout(timeout); return jedisFactory; } }
3、缓存接口 :操作redis 的服务接口
package com.fyun.common.utils; import java.io.Serializable; import java.util.concurrent.TimeUnit; /** * 缓存服务 */ public interface CacheService { void setKey(String key, Serializable obj); void setKey(String key, Serializable obj, long expireSecond); void setKey(String key, Serializable obj, long expire, TimeUnit unit); <T> T getKey(String key); boolean hasKey(String key); void delKey(String key); }
4、缓存实现
package com.fyun.common.utils.impl; import com.fyun.common.utils.CacheService; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.io.Serializable; import java.util.concurrent.TimeUnit; @Component public class DefaultCacheService implements CacheService { private RedisTemplate redisTemplate; public DefaultCacheService(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } public void setKey(String key, Serializable obj) { redisTemplate.opsForValue().set(key, obj); } public void setKey(String key, Serializable obj, long expireSecond) { setKey(key, obj, expireSecond, TimeUnit.SECONDS); } public void setKey(String key, Serializable obj, long expire, TimeUnit unit) { redisTemplate.opsForValue().set(key, obj, expire, unit); } public <T> T getKey(String key) { return (T) redisTemplate.opsForValue().get(key); } public boolean hasKey(String key) { return redisTemplate.hasKey(key); } public void delKey(String key) { redisTemplate.delete(key); } }
搞定!!!
标签:服务 public col vat ota mamicode nts img 文件
原文地址:https://www.cnblogs.com/zrboke/p/12178681.html