springboot不使用注解,以集成utils使用缓存
时间: 2023-07-30 12:05:12 浏览: 95
如果您不使用注解,可以通过以下步骤集成utils来使用缓存:
1. 添加依赖
在您的项目中添加以下依赖:
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
```
2. 配置缓存
在您的application.properties或application.yml文件中配置缓存,例如:
```yaml
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=yourpassword
```
3. 编写工具类
编写一个工具类用于缓存操作,例如:
```java
@Component
public class CacheUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
public void delete(String key) {
redisTemplate.delete(key);
}
}
```
4. 使用缓存
在您的代码中使用工具类来进行缓存操作,例如:
```java
@Autowired
private CacheUtils cacheUtils;
public void testCache() {
String key = "test";
Object value = cacheUtils.get(key);
if (value == null) {
value = "hello world";
cacheUtils.set(key, value);
}
System.out.println(value);
}
```
这样就可以使用utils来使用缓存了。需要注意的是,这里使用了Redis作为缓存,如果您使用其他缓存,需要根据实际情况进行配置。
阅读全文
相关推荐















