如何使用redis zcard方法?

时间:2022-09-11 16:31:41

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

如何在下面的程序中正确地使用StringRedisTemplate方法?

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;
        }

     }
    );
}

1 个解决方案

#1


1  

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

你可以把zCard(key)叫做RedisConnection,就像这样:

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();
  }

}); 

#1


1  

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

你可以把zCard(key)叫做RedisConnection,就像这样:

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();
  }

});