-
Notifications
You must be signed in to change notification settings - Fork 15
RxCache With Spring
Tony Shen edited this page Feb 8, 2019
·
6 revisions
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);
}
});
}
}
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);
}
});
}
}
- General
- Memory
- Persistence
-
Disk
-
Serialization
- Gson
- Fastjson
- Moshi
- Kryo
- Hessian
- FST
- Protobuf
-
Encryption
- AES 128
- DES
-
Serialization
-
Disk
- Cache Statistics
- Spring