Solon v3.0.3

solon-cache-spymemcached

</> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-cache-spymemcached</artifactId>
</dependency>

1、描述

数据扩展插件,为 Solon Data 提供基于 spymemcached 的框架适配。主要实现 CacheService 接口。

2、配置示例

#完整配置示例
demo.cache1:
  driverType: "memcached" #驱动类型
  keyHeader: "demo" #默认为 ${solon.app.name} ,可不配置
  defSeconds: 30 #默认为 30,可不配置
  server: "localhost:6379"
  password: "" #默认为空,可不配置

#简配示例
demo.cache2:
  server: "localhost:6379"

3、应用示例

//配置缓存服务
@Configuration
public class Config {
    @Bean(name = "cache1", typed = true) //typed 表示可类型注入 //即默认
    public CacheService cache1(@Inject("${demo.cache1}") MemCacheService cache){
        return cache;
    }

    @Bean(name = "cache2")
    public CacheService cache2(@Inject("${demo.cache2}") MemCacheService cache){
        return cache;
    }
    
    //通过 CacheServiceSupplier ,可根据 driverType 自动构建缓存服务
    //@Bean(name = "cache1s")
    public CacheService cache1s(@Inject("${demo.cache1}") CacheServiceSupplier supplier){
        return supplier.get();
    }
    
    //自己建构客户端  //虽然更自由,但不推荐
    //@Bean(name = "cache2s")
    public CacheService cache2s(@Inject("${demo.cache2}") Properties props){
        MemcachedClient client = new MemcachedClient(...);    
        return new MemCacheService(client, 30, "demo");
    }
}

//应用
@Controller
public class DemoController {
    //使用默认缓存服务。如果有缓存,直接返回缓存;如果没有,执行函数,缓存结果,并返回
    @Cache
    public String hello(String name) {
        return String.format("Hello {0}!", name);
    }
    
    //提醒:如果是实体,实体类要加 toString() 函数!!!
    @Cache
    public String hello2(UserModel user) { 
        return String.format("Hello {0}!", user.name);
    }
}

4、Key 使用明文

@Configuration
public class Config {
    @Bean
    public CacheService cache1(@Inject("${demo.cache1}") MemCacheService cache){
        cache.enableMd5Key(false);
        return cache;
    }
}