RedisCacheFactory.java 2.0 KB
Newer Older
Z
zhuangqian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
S
smallchill 已提交
16
package org.springblade.core.toolbox.cache;
17

18 19 20
import org.springblade.core.plugins.redis.IJedis;
import org.springblade.core.plugins.redis.Redis;

21 22 23 24 25
import java.util.ArrayList;
import java.util.List;

/**
 * Redis缓存工厂
26
 * @author zhuangqian
27
 */
Z
zhuangqian 已提交
28 29 30
public class RedisCacheFactory extends BaseCacheFactory {
	
	private IJedis jedis;
31 32 33 34 35 36 37 38 39 40 41
	
	private String redisName;
	
	public String getRedisName() {
		return redisName;
	}

	public void setRedisName(String redisName) {
		this.redisName = redisName;
	}
	
Z
zhuangqian 已提交
42 43 44 45 46 47 48 49 50 51 52
	private IJedis getCacheManager() {
		if (jedis == null) {
			synchronized (RedisCacheFactory.class) {
				if (jedis == null) {
					jedis = Redis.init(getRedisName());
				}
			}
		}
		return jedis;
	}
	
Z
zhuangqian 已提交
53 54 55 56
	public RedisCacheFactory() {
		this.redisName = "cache";
	}
	
57 58 59 60
	public RedisCacheFactory(String redisName) {
		this.redisName = redisName;
	}
	
61 62
	@Override
    public void put(String cacheName, Object key, Object value) {
Z
zhuangqian 已提交
63
		getCacheManager().hset(cacheName, key, value);
64 65
	}

66 67
	@Override
    public <T> T get(String cacheName, Object key) {
Z
zhuangqian 已提交
68
		return getCacheManager().hget(cacheName, key);
69 70
	}

71 72
	@Override
    @SuppressWarnings("rawtypes")
73
	public List getKeys(String cacheName) {
Z
zhuangqian 已提交
74
		return new ArrayList<>(getCacheManager().hkeys(cacheName));
75 76
	}

77 78
	@Override
    public void remove(String cacheName, Object key) {
Z
zhuangqian 已提交
79
		getCacheManager().hdel(cacheName, key);
80 81
	}

82 83
	@Override
    public void removeAll(String cacheName) {
Z
zhuangqian 已提交
84
		getCacheManager().del(cacheName);
85 86 87
	}

}