Redis Study Notes (4) [Original]

Recently, I prepared a framework package based on spring-data-redis to encapsulate the company's access to the cache, and found several serious problems.
1. Weak support for advanced methods such as scan
   If you want to implement the scan method, you need to call the redisTemplate.execute(RedisCallBack) method.
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;
            }

        });

After the actual measurement, it was found that scanCount has no effect at all. No matter what is passed, the total amount is returned, which is very time-consuming. At the same time, the usage of scan is to set the starting point of the next scan according to the returned number, so that the time-consuming command of keys is replaced by iteration. Unfortunately, ScanOptions does not support the start parameter, and the RedisConnection class has only one options parameter, so this command It's the same as keys. When checking a large amount of data, it is very time-consuming. I decompiled it and saw that the code inside is written like this.
#org.springframework.data.redis.connection.jedis.JedisConnection
public Cursor<byte[]> scan(ScanOptions options)
  {
    return scan(0L, ScanOptions.NONE);
  }

Then what is the purpose of passing in the options? ^_^.

2. There are really very few documents.
Du Niang basically only teaches how to build the environment, how to operate the ordinary value, set, map, and list, and there is almost no in-depth knowledge. Then look at the official website. The following Command Reference lists a bunch of commands that RedisTemplate does not support. Then these can only be packaged by themselves.

3. Poor performance
#Use spring-data-redis code
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;
	}
}


#Native jedis package code
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("Use native 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);
    }
}


The size of other connection pools and parameter configurations are exactly the same.

After actual measurement:
1) spring-data-redis testOnBorrow=true about 28 seconds
2) spring-data-redis testOnBorrow=false about 20 seconds
3) native jedis packaging takes about 6 seconds, so

it is recommended to use Native jedis package, easy to control, good performance, a lot of documentation online.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326168657&siteId=291194637