Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
gu.sql2java.redis.cache.RedisCaches Maven / Gradle / Ivy
package gu.sql2java.redis.cache;
import static com.google.common.base.Strings.isNullOrEmpty;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import gu.simplemq.redis.RedisTable;
import gu.sql2java.BaseBean;
/**
* REDIS数据库缓存项目工具类,定义公用的静态方法
* @author guyadong
*
*/
public class RedisCaches {
/**
* 在REDIS中保存表记录时KEY的字符串分割符默认值
*/
public static String DEFAULT_SEPARATOR = ":";
/**
* 在REDIS中保存表记录时KEY的统一前缀默认值
*/
public static String DEFAULT_CACHE_KEY_PREFIX="DBCACHE" + DEFAULT_SEPARATOR;
/**
* 在REDIS中保存表记录时KEY的字符串分割符
*/
private static String separator;
static {
setSeparator(DEFAULT_SEPARATOR);
}
/**
* 在REDIS中保存表记录时KEY的统一前缀
*/
private static String cacheKeyPrefix = DEFAULT_CACHE_KEY_PREFIX;
@SuppressWarnings("rawtypes")
private static final Table,String,RedisCache> cacheTable = HashBasedTable.create();
private static final Table readerTable = HashBasedTable.create();
private RedisCaches(){}
/**
* 定义在REDIS中保存表记录时KEY的统一前缀,如果未指定则使用默认值{@link #DEFAULT_CACHE_KEY_PREFIX}
* @param cacheKeyPrefix 为{@code null}或空忽略
*/
public static void setCacheKeyPrefix(String cacheKeyPrefix) {
if(!isNullOrEmpty(cacheKeyPrefix)){
RedisCaches.cacheKeyPrefix = cacheKeyPrefix;
}
}
public static void setSeparator(String separator) {
if(!isNullOrEmpty(separator)){
RedisCaches.separator = separator;
RedisTable.setKeySeparator(separator);
}
}
/**
* 返回在REDIS中保存表记录时KEY的统一前缀
*/
public static String getCacheKeyPrefix() {
return cacheKeyPrefix;
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})根据类名和字段名创建频道名
* @param beanClassName
* @param keyName
*/
public static String channelNameOf(String beanClassName, String keyName){
return channelNameOf(cacheKeyPrefix,beanClassName,keyName);
}
/**
*
* 根据类名和字段名创建频道名
* @param keyPrefix KEY的统一前缀,为{@code null}或空使用默认值{@link #cacheKeyPrefix}
* @param beanClassName
* @param keyName
*/
public static String channelNameOf(String keyPrefix,String beanClassName, String keyName){
if(isNullOrEmpty(keyPrefix)){
keyPrefix = cacheKeyPrefix;
}
if(!isNullOrEmpty(beanClassName) && !isNullOrEmpty(keyName)){
return keyPrefix + beanClassName + separator + keyName;
}
return null;
}
/**
* 创建{@code beanClass}指定数据库表的REDSI 缓存机制
* 如果已经创建则忽略
* @param keyPrefix KEY的统一前缀,为{@code null}或空使用默认值{@link #cacheKeyPrefix}
* @param beanClass 数据库表记录类
* @param columnName 索引字段名,必须为单字段的主键或唯一键字段名,
* @param jsonFormatter Java Bean到JSON转换器实例,为{@code null}使用默认转换所有字段的实例,
* @return REDIS缓存实例
*/
@SuppressWarnings("unchecked")
public synchronized static RedisCache createCache(String keyPrefix,Class beanClass,String columnName, Function jsonFormatter){
RedisCache cache;
if(null == (cache = cacheTable.get(beanClass, columnName))){
cache = new RedisCache<>(keyPrefix, beanClass, columnName)
.setJsonFormater(jsonFormatter);
cacheTable.put(beanClass, columnName, cache);
}
return cache;
}
/**
* @see #createCache(String, Class, String, Function)
*/
public static RedisCache createCache(String keyPrefix,Class beanClass, String columnName){
return createCache(keyPrefix,beanClass, columnName,null);
}
/**
* 创建{@code beanClass}指定数据库表的以主键为索引的REDSI 缓存实例
* @see #createCache(String ,Class, String, Function)
* @return REDIS缓存实例
*/
public static RedisCache createCache(String keyPrefix, Class beanClass, Function jsonFormatter){
return createCache(keyPrefix,beanClass,null, jsonFormatter);
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})创建{@code beanClass}指定数据库表的REDSI 缓存实例
* 如果已经创建则忽略
* @param beanClass 数据库表记录类
* @param columnName 索引字段名,必须为单字段的主键或唯一键字段名,
* @param jsonFormatter Java Bean到JSON转换器实例,为{@code null}使用默认转换所有字段的实例,
* @return REDIS缓存实例
*/
public static RedisCache createCache(Class beanClass,String columnName, Function jsonFormatter){
return createCache(null,beanClass,columnName,jsonFormatter);
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})创建{@link RedisCache}实例
* @see #createCache(String, Class, String, Function)
*/
public static RedisCache createCache(Class beanClass, String columnName){
return createCache(null,beanClass, columnName,null);
}
/**
* 创建{@code beanClass}指定数据库表的REDSI 缓存机制
* 如果已经创建则忽略
* @param keyPrefix KEY的统一前缀,为{@code null}或空使用默认值{@link #cacheKeyPrefix}
* @param beanClass 数据库表记录类
* @param jsonFormatter Java Bean到JSON转换器实例,为{@code null}使用默认转换所有字段的实例,
* @param columnNames 索引字段名列表,必须为单字段的主键或唯一键字段名,为{@code null}忽略
* @return 返回{@link Caches}实例映射
*/
public static Caches
createCaches(String keyPrefix,Class beanClass,Function jsonFormatter, Iterable columnNames){
Caches m = new Caches<>();
if(null != columnNames){
Set names = Sets.newHashSet(Iterables.filter(columnNames,Predicates.notNull()));
for(String columnName : names){
m.put(columnName,createCache(keyPrefix,beanClass,columnName,jsonFormatter));
}
}
return m;
}
/**
* @see #createCaches(String, Class, Function, Iterable)
*/
public static Caches
createCaches(String keyPrefix,Class beanClass,Function jsonFormatter, String... columnNames){
if(null != columnNames){
return createCaches(keyPrefix, beanClass, jsonFormatter, Arrays.asList(columnNames));
}
return new Caches<>();
}
/**
* @see #createCaches(String, Class, Function, Iterable)
*/
public static Caches
createCaches(String keyPrefix,Class beanClass, Iterable columnNames){
return createCaches(keyPrefix,beanClass, null,columnNames);
}
/**
* @see #createCaches(String, Class, Function, Iterable)
*/
public static Caches
createCaches(String keyPrefix,Class beanClass, String... columnNames){
return createCaches(keyPrefix,beanClass, null,columnNames);
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})创建{@code beanClass}指定数据库表的REDSI 缓存实例
* 如果已经创建则忽略
* @param beanClass 数据库表记录类
* @param jsonFormatter Java Bean到JSON转换器实例,为{@code null}使用默认转换所有字段的实例,
* @param columnNames 索引字段名列表,必须为单字段的主键或唯一键字段名
*/
public static Caches
createCaches(Class beanClass,Function jsonFormatter, Iterable columnNames){
return createCaches(null,beanClass,jsonFormatter,columnNames);
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})创建{@code beanClass}指定数据库表的REDSI 缓存实例
* 如果已经创建则忽略
* @param beanClass 数据库表记录类
* @param jsonFormatter Java Bean到JSON转换器实例,为{@code null}使用默认转换所有字段的实例,
* @param columnNames 索引字段名列表,必须为单字段的主键或唯一键字段名
*/
public static Caches
createCaches(Class beanClass,Function jsonFormatter, String... columnNames){
return createCaches(null,beanClass,jsonFormatter,columnNames);
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})创建{@code beanClass}指定数据库表的REDSI 缓存实例
* 如果已经创建则忽略
* @param beanClass 数据库表记录类
* @param columnNames 索引字段名列表,必须为单字段的主键或唯一键字段名
*/
public static Caches
createCaches(Class beanClass, Iterable columnNames){
return createCaches(null,beanClass,null,columnNames);
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})创建{@code beanClass}指定数据库表的REDSI 缓存实例
* 如果已经创建则忽略
* @param beanClass 数据库表记录类
* @param columnNames 索引字段名列表,必须为单字段的主键或唯一键字段名
*/
public static Caches
createCaches(Class beanClass, String... columnNames){
return createCaches(null,beanClass,null,columnNames);
}
/**
* 返回{@code beanClass}指定数据库表的REDSI 缓存读取实例
* 如果没有找到则创建新实例
* @param keyPrefix KEY的统一前缀,为{@code null}或空使用默认值{@link #cacheKeyPrefix}
* @param beanClassName 数据库表记录类名
* @param columnName 索引字段名,必须为单字段的主键或唯一键字段名,
*/
public synchronized static
RedisCacheReader createCacheReader(String keyPrefix,String beanClassName,String columnName){
RedisCacheReader reader;
if(null == (reader = readerTable.get(beanClassName, columnName))){
reader = new RedisCacheReader(keyPrefix,beanClassName, columnName);
readerTable.put(beanClassName, columnName, reader);
}
return reader;
}
/**
* 使用默认前缀({@link #getCacheKeyPrefix()})返回{@code beanClass}指定数据库表的REDSI 缓存读取实例
* 如果没有找到则创建新实例
* @param beanClassName 数据库表记录类名
* @param columnName 索引字段名,必须为单字段的主键或唯一键字段名,
*/
public static
RedisCacheReader createCacheReader(String beanClassName,String columnName){
return createCacheReader(null,beanClassName,columnName);
}
/**
* 编译管理一个表下主键及唯一键缓存配置
* @author guyadong
*
* @param
*/
public static class Caches extends HashMap>{
private static final long serialVersionUID = 1L;
private Caches() {
}
/**
* @see RedisCache#jsonFields(int...)
* @return 当前对象
*/
public Caches jsonFields(int... jsonFields) {
for(RedisCache value:values()){
value.jsonFields(jsonFields);
}
return this;
}
/**
* 配置数据库对象转换为JSON时类型为JSON的字段名列表
* @see RedisCache#jsonFields(String...)
* @return 当前对象
*/
public Caches jsonFields(String... jsonFields) {
for(RedisCache value:values()){
value.jsonFields(jsonFields);
}
return this;
}
/**
* @see RedisCache#jsonFields(Iterable)
* @return 当前对象
*/
public Caches jsonFields(IterablejsonFields) {
for(RedisCache value:values()){
value.jsonFields(jsonFields);
}
return this;
}
/**
* 配置数据库对象转换为JSON时的输出字段
* @see RedisCache#columns(boolean, Iterable)
* @return 当前对象
*/
public Caches columns(boolean include,Iterable columns) {
for(RedisCache value:values()){
value.columns(include,columns);
}
return this;
}
/**
* @see #columns(boolean, Iterable)
*/
public Caches columns(boolean include,String ... columns) {
for(RedisCache value:values()){
value.columns(include,columns);
}
return this;
}
/**
* @see RedisCache#start()
* @return 当前对象
*/
public Caches start(){
for(RedisCache value:values()){
value.start();
}
return this;
}
}
}