stanhangzhou
stanhangzhou

Reputation: 3

How StringRedisTemplate use redis zcard method?

How would I properly use the StringRedisTemplate method in this following program?

public void put(StringRedisTemplate template, final Object key, final Object value ) {
    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
             connection.zCard((byte[]) key);

             connection.exec();
             return null;
        }

     }
    );
}

Upvotes: 0

Views: 463

Answers (1)

Christoph Strobl
Christoph Strobl

Reputation: 6736

You can call zCard(key) on RedisConnection somehow like this:

Long zCard = template.execute(new RedisCallback<Long>() {

  @Override
  public Long doInRedis(RedisConnection connection) throws DataAccessException {
    return connection.zCard(potentiallyExtractBytes(key));
  }

  private byte[] potentiallyExtractBytes(Object key) {
    return (key instanceof byte[]) ? (byte[]) key : key.toString().getBytes();
  }

}); 

Upvotes: 1

Related Questions