import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.serializer.*; import java.time.Duration; /** * Redis 配置类 *...
SDR(spring-data-redis)的官方讲解如下 https://docs.spring.io/spring-data/redis/docs/1.8.1.RELEASE/reference/html/#redis:template 缓存的配置如下 在RedisCacheConfig上添加注解 创建RedisCacheManager 自定义缓存的key 在RedisCacheConfig中添加以上的代码,就可以使用springcache的注解了。下面介绍springcache的注解...
packagecom.swd.mydemo.service;importcom.swd.mydemo.bean.User;importcom.swd.mydemo.dao.UserDao;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.CachePut;importorg.springframework.cache.annotation.Cacheable;importorg.springframework.stereotype.Service;/**...
@Service@CacheConfig(cacheNames ="user")public class UserServiceImpl implements UserService {/** * 新增用户 */publicUseraddUser(User user) { ... }/** * 查询用户 */@Cacheable(key ="#username") public User getUserByUsername(String username) { ... }/** * 更新用户 */@CachePut(key ...
Spring Cache 常用 API @Cacheable @Cacheable: 用于标记一个方法的结果应该被缓存。当在该方法被调用时,Spring首先检查缓存中是否已经有了预期的结果,如果有,直接返回缓存中的结果而不执行实际的方法体。 javaCopy code@Cacheable(cacheNames = "exampleCache", key = "'exampleKey'")public String getCachedData...
Spring Cache 常用 API @Cacheable @Cacheable:用于标记一个方法的结果应该被缓存。当在该方法被调用时,Spring首先检查缓存中是否已经有了预期的结果,如果有,直接返回缓存中的结果而不执行实际的方法体。 javaCopy code@Cacheable(cacheNames="exampleCache",key="'exampleKey'")publicStringgetCachedData(){// 执...
CacheConfig.java 这个缓存的配置 packagecom.example.demo.config;importcom.example.demo.constant.CacheConstant;importcom.google.common.cache.CacheBuilder;importcom.google.common.collect.Lists;importnet.sf.ehcache.config.CacheConfiguration;importnet.sf.ehcache.store.MemoryStoreEvictionPolicy;importorg.springframew...
getUserById – 方法的返回结果会被缓存到redis,使用注解@CacheableupdateUserNickname – 原始数据被更新了,废弃缓存数据,使用注解@CacheEvict UserSevice.java 接口 public interface UserService { public User getUserById(long userId); public User updateUserNickname(long userId, String nickname);} ...
Spring 3.1引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache或者Redis),而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的各种annotation,即能够达到缓存方法的返回对象的效果。 <!-- more --> ...
spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.database=0 spring.redis.password=xxx 新增KeyGeneratorCacheConfig.java(或者名为CacheConfig)文件 该文件完成三项设置:key过期时间;key命名方式;value序列化方式:JSON便于查看 ...