All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.oschina.j2cache.cache.support.redis.SpringRedisCache Maven / Gradle / Ivy

There is a newer version: 2.8.0-release
Show newest version
package net.oschina.j2cache.cache.support.redis;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import net.oschina.j2cache.Level2Cache;

/**
 * 重新实现二级缓存
 * 
 * @author zhangsaizz
 *
 */
public class SpringRedisCache implements Level2Cache {

	private String namespace;

	private String region;

	private RedisTemplate redisTemplate;

	public SpringRedisCache(String namespace, String region, RedisTemplate redisTemplate) {
		if (region == null || region.isEmpty()) {
			region = "_"; // 缺省region
		}
		this.namespace = namespace;
		this.redisTemplate = redisTemplate;
		this.region = getRegionName(region);
	}

	private String getRegionName(String region) {
		if (namespace != null && !namespace.isEmpty())
			region = namespace + ":" + region;
		return region;
	}

	@Override
	public void clear() {
		redisTemplate.opsForHash().delete(region);
	}

	@Override
	public boolean exists(String key) {
		return redisTemplate.opsForHash().hasKey(region, key);
	}

	@Override
	public void evict(String... keys) {
		for (String k : keys) {
			if (!k.equals("null")) {
				redisTemplate.opsForHash().delete(region, k);		
			} else {
				redisTemplate.delete(region);
			}
		}
	}

	@Override
	public Collection keys() {
		Set list = redisTemplate.opsForHash().keys(region);
		List keys = new ArrayList<>(list.size());
		for (Object object : list) {
			keys.add((String) object);
		}
		return keys;
	}

	@Override
	public byte[] getBytes(String key) {
		return redisTemplate.opsForHash().getOperations().execute((RedisCallback) redis -> redis.hGet(region.getBytes(), key.getBytes()));	
	}

	@Override
	public List getBytes(Collection keys) {
		return redisTemplate.opsForHash().getOperations().execute((RedisCallback>) redis -> {
			byte[][] bytes = keys.stream().map(k -> k.getBytes()).toArray(byte[][]::new);
			return redis.hMGet(region.getBytes(), bytes);
		});
	}

	@Override
	public void put(String key, Object value) {
		redisTemplate.opsForHash().put(region, key, value);
	}

    /**
     * 设置缓存数据的有效期
     */
	@Override
	public void put(String key, Object value, long timeToLiveInSeconds) {
        redisTemplate.opsForHash().put(region, key, value);
    }

	@Override
	public void setBytes(String key, byte[] bytes) {
		redisTemplate.opsForHash().getOperations().execute((RedisCallback>) redis -> {
			redis.set(_key(key).getBytes(), bytes);
			redis.hSet(region.getBytes(), key.getBytes(), bytes);
			return null;
		});
	}

	@Override
	public void setBytes(Map bytes) {
		bytes.forEach((k, v) -> {
			setBytes(k, v);
		});
	}
	
	private String _key(String key) {
		return this.region + ":" + key;
	}
	
}