Redis 学习笔记(四) 【原创】

最近准备基于spring-data-redis封装公司访问缓存的框架包,发现几个比较严重的问题。
1. 对于scan等高级方法支持较弱
   如果要实现scan方法,那么需要调用redisTemplate.execute(RedisCallBack)方法。
List<String> list=redisTemplate.execute(new RedisCallback<List<String>>() {
            @Override
            public List<String> doInRedis(RedisConnection connection) throws DataAccessException {
                ScanOptions options = ScanOptions.scanOptions().match(scanPattern).count(scanCount).build();
                Cursor<byte[]> entries = connection.scan(options);
                List<String> result = new ArrayList<String>();
                if(entries!=null)
                    while(entries.hasNext()){
                        result.add(new String(entries.next()));
                    }
                return result;
            }

        });

经过实测发现,scanCount根本没作用,无论传什么都是把总量返回,这就导致非常耗时。同时scan的用法,本来是根据返回的数量设置下一次scan的起点,这样通过迭代取代keys这种耗时命令,不幸的是ScanOptions并不支持start参数,同时RedisConnection类只有一个options参数,所以这个命令就是跟keys一样的。当查大数据量的时候,非常耗时。反编译了一下,看到里面的代码是这么写的。
#org.springframework.data.redis.connection.jedis.JedisConnection
public Cursor<byte[]> scan(ScanOptions options)
  {
    return scan(0L, ScanOptions.NONE);
  }

那这个options传入是干啥用的呢,^_^.

2.文档真的非常少
度娘基本只教环境怎么搭建,普通的value, set, map, list怎么操作,深入一点的几乎没有。然后看官网, 下面的  Command Reference 列出了一堆RedisTemplate不支持的命令。那这些只能自己封装了。

3.性能比较差
#使用spring-data-redis代码
public static void main(String[] args) {
        ApplicationContext ac =  new ClassPathXmlApplicationContext("classpath:/config/spring-redis-sentinel.xml");
        RedisCache redisCache = (RedisCache) ac.getBean("redisCache");
        long start=System.currentTimeMillis();
        for(int i=0;i<10000;i++){
        	redisCache.set("USER_ID"+i, "zhenggm");
        }
        System.out.print("采用spring-data-redis:"+(System.currentTimeMillis()-start));
    }

public class RedisCache {
	
	private StringRedisTemplate redisTemplate;
	
	public String get(String key){
        return redisTemplate.boundValueOps(key).get();
    }
	
	public void set(String key, String value){
        redisTemplate.opsForValue().set(key ,value);
	}

	public StringRedisTemplate getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(StringRedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
}


#原生jedis封装代码
public static void main(String[] args) {
		new ClassPathXmlApplicationContext("classpath:/config/spring-jedis.xml");
		RedisCache cache=RedisCacheFactory.getCacheResource();
		long start=System.currentTimeMillis();
        for(int i=0;i<10000;i++){
        	cache.set("USER_ID"+i, "zhenggm");
        }
        System.out.print("采用原生jedis:"+(System.currentTimeMillis()-start));
        RedisCacheFactory.close(cache);
    }

public class RedisCache{
    private final Jedis jedis;


    public String set(String key, String value){
    	return this.jedis.set(key, value);
    }
}


其他连接池大小,参数配置一模一样

经过实测:
1)spring-data-redis testOnBorrow=true  28秒左右
2)spring-data-redis testOnBorrow=false 20秒左右
3)原生jedis分装                          6秒左右

所以建议还是采用原生jedis封装,易掌控,性能好,文档网上一搜一大把。



猜你喜欢

转载自zhenggm.iteye.com/blog/2408478