使用 Spring 来配置 RxCache
使用 Spring 来配置 RxCache
package spring;
import com.safframework.rxcache.RxCache;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
/**
* Created by tony on 2018/10/5.
*/
@Configurable
public class Config {
@Bean
public RxCache.Builder rxCacheBuilder(){
return new RxCache.Builder();
}
@Bean
public RxCache rxCache() {
RxCache.config(rxCacheBuilder());
return RxCache.getRxCache();
}
}
通过 ApplicationContext 的 getBean() 方法来获取 RxCache。
package spring;
import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.domain.Record;
import domain.User;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by tony on 2018/10/5.
*/
public class TestWithSpring {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
RxCache rxCache = (RxCache) ctx.getBean("rxCache");
User u = new User();
u.name = "tony";
u.password = "123456";
rxCache.save("test",u);
Observable<Record<User>> observable = rxCache.load2Observable("test", User.class);
observable.subscribe(new Consumer<Record<User>>() {
@Override
public void accept(Record<User> record) throws Exception {
User user = record.getData();
System.out.println(user.name);
System.out.println(user.password);
}
});
}
}
跟 Guava 整合
Memory 的实现使用 GuavaCacheImpl
import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.extra.memory.GuavaCacheImpl;
import com.safframework.rxcache.memory.Memory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
/**
* Created by tony on 2018/10/5.
*/
@Configurable
public class ConfigWithGuava {
@Bean
public Memory guavaCache(){
return new GuavaCacheImpl(100);
}
@Bean
public RxCache.Builder rxCacheBuilder(){
return new RxCache.Builder().memory(guavaCache());
}
@Bean
public RxCache rxCache() {
RxCache.config(rxCacheBuilder());
return RxCache.getRxCache();
}
}
测试一下刚才的整合:
package spring;
import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.domain.Record;
import domain.User;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by tony on 2018/10/5.
*/
public class TestWithGuava {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithGuava.class);
RxCache rxCache = (RxCache) ctx.getBean("rxCache");
User u = new User();
u.name = "tony";
u.password = "123456";
rxCache.save("test",u);
Observable<Record<User>> observable = rxCache.load2Observable("test", User.class);
observable.subscribe(new Consumer<Record<User>>() {
@Override
public void accept(Record<User> record) throws Exception {
User user = record.getData();
System.out.println(user.name);
System.out.println(user.password);
}
});
}
}
相关文章
- 基于-SLF4J-MDC-机制的日志链路追踪配置属性
ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC
- ajax-跨域访问
ajax 跨域访问 <!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head>
- 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache
spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port
- Java动态代理
Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public
- Java读取classpath中的文件
public void init() { try { //URL url = Thread.currentThread().getContextClassLo
随机推荐
- 基于-SLF4J-MDC-机制的日志链路追踪配置属性
ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC
- ajax-跨域访问
ajax 跨域访问 <!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head>
- 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache
spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port
- Java动态代理
Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public
- Java读取classpath中的文件
public void init() { try { //URL url = Thread.currentThread().getContextClassLo