All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hetianyi.common.cache.README.md Maven / Gradle / Ivy

### 缓存组件使用手册

#### 使用

```java
@Service
public class UserService implements IUserService {

    // 缓存结果
    // 缓存的key为user_info:
    @Cacheable(key = "'user_info:' + #args[0]", expire = 60000)
    @Override
    public UserInfoVO getUserInfo(Long userId) {
        return new UserInfoVO(userId, 1L, "李斯特", "主管");
    }

    @Cacheable(key = "'user_info1:' + #args[0].userId", expire = 60000, compressMinSize = 50)
    @Override
    public UserInfoVO getUserInfo1(UserInfoVO info) {
        return info;
    }

    // 在删除、更新等方法上使缓存失效
    // 缓存的key为user_info:
    @ExpireCache(key = "'user_info:' + #args[0]")
    @Override
    public Boolean delUserInfo(Long userId) {
        return true;
    }
}
```


> 在springboot里进行配置

```java
@Configuration
public class CacheConfig {

    @Autowired
    private RedisConnectionFactory factory;

    /**
     * 开启缓存
     */
    @Order(1)
    @Aspect
    @Configuration
    public class CacheableConfig extends CacheableAspectResolver {
        @Override
        public RedisConnectionFactory redisConnectionFactory() {
            return factory;
        }
    }

    /**
     * 开启缓存失效
     */
    @Order(1)
    @Aspect
    @Configuration
    public class ExpireCacheConfig extends ExpireCacheAspectResolver {
        @Override
        public RedisConnectionFactory redisConnectionFactory() {
            return factory;
        }
    }
}
```






© 2015 - 2024 Weber Informatics LLC | Privacy Policy