服务开通及简单使用

1、进入开发者后台,进入开发管理下的开发资源界面,并点击开通。

2、保存生成的appSecret和appId值。

3、开通缓存服务。

4、把刚才获取到的APP ID和APP Secret配置到工程管理的配置中心;如果不配置,则调用本地redis服务。

5、工程引入pai-common-cache依赖包。

6、导入封装好的RedisService,该服务类提供了常规redis命令。


前缀配置

申请前缀


在这里可自定义更新工程项目的前缀?

全局配置

此为全局的共享配置提供,工程项目可忽略,默认值为 pai:
默认超时时间为:7200秒

public class CacheKeyPrefixProperties {

    /**
     * 默认超时时间(单位:秒)
     */
    @Value("${pai.redis.timeout:7200}")
    Long timeToLive;

    /**
     * 全局前缀
     */
    @Value("${pai.redis.prefix:pai}")
    String globalKeyPrefix;

}

使用示例

注意

  • 使用 @Cacheable @CachePut @CacheEvict等注释时需要手动在工程启动增加 @EnableCaching
  • 当现出跨套件使用缓存对象时要写上完整的前缀在cacneNames上,可以考虑使用Nacos配置+静态变量方式使用,避免修改代码
@SpringBootApplication
@EnableCaching
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

定义接口


public interface ITestSerivce {

    void cacheEvict();

    String cachePut(String value);

    Map<String, String> cacheable(Map<String, String> map);

    Person caching(String personName);

    String ttl(String key);

}

接口实现

@Service
public class TestServiceImpl implements ITestSerivce {

    @CacheEvict(value = "joe", allEntries = true)
    @Override
    public void cacheEvict() {
        System.out.println("cacheEvict joe:*");
    }

    @CachePut(value = "joe:put", key = "#value", cacheResolver = CacheExpire.REDIS_EXPIRE_CACHE_RESOLVER)
    @CacheExpire(expire = 101)
    @Override
    public String cachePut(String value) {
        System.out.println("cachePut " + value);

        return value + " world";
    }

    /**
     * 注意:@Cacheable注解的key属性中不能使用#result取出方法执行后的返回值,
     * 因为@Cacheable注解是先到缓存中查找,在执行方法的,在没执行方法前,#result是取不到值的。
     *
     * @param map
     * @return
     */
    @Override
    @Cacheable(key = "#map['key1']+'-'+#map['key2']", cacheNames = "joe:cacheable")
    public Map<String, String> cacheable(Map<String, String> map) {
        System.out.println("cacheable " + map.get("key1"));
        Map<String, String> data = new HashMap<>(map);
        data.put("key2", "keys2");
        return data;
    }

    @Override
    @Caching(
            cacheable = {
                    @Cacheable(value = "joe:caching:person4", key = "#personName")
            },
            put = {
                    @CachePut(value = "joe:caching:person1", key = "#result.personId", cacheResolver = CacheExpire.REDIS_EXPIRE_CACHE_RESOLVER),
                    @CachePut(value = "joe:caching:person2", key = "#result.personPs")
            },
            evict = {
                    @CacheEvict(value = "joe:caching:person2", key = "#result.personPs")
            }
    )
    @CacheExpire(expire = 50, timeUnit = CacheExpire.TimeUnit.MINUTES)
    public Person caching(String personName) {
        System.out.println(personName);

        Person person = new Person();
        person.setPersonId("001");
        person.setPersonName(personName);
        person.setPersonPs("002");

        return person;
    }

    /**
     * 使用全局默认过期时间(7200秒)
     */
    @Override
    @Cacheable(value = "ttl")
    public String ttl(String key) {
        return "k_" + key;
    }

}

非接口类

@Component
public class TestUtil {

    /**
     * 本套件内操作ttl:ttl-day的缓存示例
     */
    @CacheExpire(expire = 1, timeUnit = CacheExpire.TimeUnit.DAYS)
    @Cacheable(value = "ttl", key = "'ttl-day'", cacheResolver = CacheExpire.REDIS_EXPIRE_CACHE_RESOLVER)
    public String ttl(String key) {
        return "k_" + key;
    }

    /**
     * 跨套件调用示例
     * <pre>
     * 全局前缀:flyrise
     * 套件前缀:demo
     * 缓存Key:ttl:ttl-day
     * </pre>
     */
    @CacheExpire(expire = 2, timeUnit = CacheExpire.TimeUnit.DAYS)
    @CachePut(value = "flyrise:demo:ttl:", key = "'ttl-day'", cacheResolver = CacheExpire.REDIS_EXPIRE_CACHE_RESOLVER)
    public String otherProject(String key) {
        return "other caller k_" + key;
    }
}

直接使用redisTemplate

public class RedisTest {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate2;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void useRedisTemplate() {
        String name = (String) redisTemplate.opsForValue().get("joe:name");
        System.out.println(name);

        redisTemplate.opsForValue().set("joe:name", "admin", 200, TimeUnit.SECONDS);
        name = (String) redisTemplate.opsForValue().get("joe:name");
        System.out.println(name);

        redisTemplate.opsForValue().set("joe:age", "1111", 200, TimeUnit.SECONDS);
        String age = (String) redisTemplate.opsForValue().get("joe:age");
        System.out.println(age);

        if (Boolean.TRUE.equals(redisTemplate.hasKey("joe:age"))) {
            System.out.println("删除 joe:age");
            redisTemplate.delete("joe:age");
        }


        redisTemplate2.opsForValue().set("joe:name-object", "admin-ogject", 200, TimeUnit.SECONDS);
        name = (String) redisTemplate2.opsForValue().get("joe:name-object");
        System.out.println("joe:name-object" + name);

        stringRedisTemplate.opsForValue().set("joe:name-string", "admin-string", 200, TimeUnit.SECONDS);
        name = (String) stringRedisTemplate.opsForValue().get("joe:name-string");
        System.out.println("joe:name-string" + name);
    }
}
文档更新时间: 2022-11-29 11:32   作者:管理员