版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接
maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
启动类
@SpringBootApplication
@EnableCaching
public class CollectionApplication {
public static void main(String[] args) {
SpringApplication.run(CollectionApplication.class, args);
}
}
service:
@Service
@CacheConfig(cacheNames = "myCache")
public class CacheServiceImpl implements CacheService {
//操作字符串
@Autowired
private StringRedisTemplate stringRedisTemplate;
//操作对象
@Autowired
RedisTemplate redisTemplate;
@Override
@Cacheable(key = "targetClass + methodName +#p0")
public User queryUser(String username) {
User user = new User();
user.setAddress("北京");
user.setAge(26);
user.setBirthday(new Date());
user.setUserName(username);
//stringRedisTemplate.opsForValue().set("username","ethan");
return user;
}
}
再次访问就会调用redis中的值。
redis-cli 里查看key
xxxx:8001> scan 0 match * count 10
1) "0"
2) 1) "myCache::class com.qjs.service.impl.CacheServiceImplqueryUserethan"
xxxx:8001>