redis.clients.jedis.Jedis.ltrim()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(5.6k)|赞(0)|评价(0)|浏览(234)

本文整理了Java中redis.clients.jedis.Jedis.ltrim()方法的一些代码示例,展示了Jedis.ltrim()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jedis.ltrim()方法的具体详情如下:
包路径:redis.clients.jedis.Jedis
类名称:Jedis
方法名:ltrim

Jedis.ltrim介绍

[英]Trim an existing list so that it will contain only the specified range of elements specified. Start and end are zero-based indexes. 0 is the first element of the list (the list head), 1 the next element and so on.

For example LTRIM foobar 0 2 will modify the list stored at foobar key so that only the first three elements of the list will remain.

start and end can also be negative numbers indicating offsets from the end of the list. For example -1 is the last element of the list, -2 the penultimate element and so on.

Indexes out of range will not produce an error: if start is over the end of the list, or start > end, an empty list is left as value. If end over the end of the list Redis will threat it just like the last element of the list.

Hint: the obvious use of LTRIM is together with LPUSH/RPUSH. For example:

lpush("mylist", "someelement"); ltrim("mylist", 0, 99); *

The above two commands will push elements in the list taking care that the list will not grow without limits. This is very useful when using Redis to store logs for example. It is important to note that when used in this way LTRIM is an O(1) operation because in the average case just one element is removed from the tail of the list.

Time complexity: O(n) (with n being len of list - len of range)
[中]修剪现有列表,使其仅包含指定的元素范围。开始和结束是基于零的索引。0是列表的第一个元素(列表头),1是下一个元素,依此类推。
例如,LTRIM foobar 0 2将修改存储在foobar键处的列表,以便仅保留列表的前三个元素。
“开始”和“结束”也可以是负数,表示从列表末尾开始的偏移。例如,-1是列表的最后一个元素,-2是倒数第二个元素,依此类推。
超出范围的索引不会产生错误:如果开始超出列表的末尾,或开始>结束,则留下一个空列表作为值。如果结束在列表的末尾,Redis将像列表的最后一个元素一样威胁它。
提示:LTRIM的明显用法是与LPUSH/RPUSH一起使用的。例如:
lpush(“mylist”、“someelement”);ltrim(“mylist”,0,99)*
以上两个命令将推动列表中的元素,注意列表不会无限增长。例如,当使用Redis存储日志时,这非常有用。重要的是要注意,当以这种方式使用LTRIM时,它是一个O(1)操作,因为在一般情况下,只有一个元素从列表的尾部删除。
时间复杂度:O(n)(n是列表的len-范围的len)

代码示例

代码示例来源:origin: sohutv/cachecloud

@Override
 public String execute(Jedis connection) {
  return connection.ltrim(key, start, end);
 }
}.runBinary(key);

代码示例来源:origin: sohutv/cachecloud

@Override
 public String execute(Jedis connection) {
  return connection.ltrim(key, start, end);
 }
}.run(key);

代码示例来源:origin: sohutv/cachecloud

@Override
public String ltrim(String key, long start, long end) {
 Jedis j = getShard(key);
 return j.ltrim(key, start, end);
}

代码示例来源:origin: sohutv/cachecloud

@Override
public String ltrim(byte[] key, long start, long end) {
 Jedis j = getShard(key);
 return j.ltrim(key, start, end);
}

代码示例来源:origin: Netflix/conductor

@Override
public String ltrim(String key, long start, long end) {
 Jedis jedis = null;
  try {
   jedis = jedisPool.getResource();
   return jedis.ltrim(key, start, end);
  } finally {
   if (jedis != null)
    jedis.close();
  }
}

代码示例来源:origin: jfinal/jfinal

/**
 * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
 * 举个例子,执行命令 LTRIM list 0 2 ,表示只保留列表 list 的前三个元素,其余元素全部删除。
 * 下标(index)参数 start 和 stop 都以 0 为底,也就是说,以 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。
 * 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
 * 当 key 不是列表类型时,返回一个错误。
 */
public String ltrim(Object key, long start, long end) {
  Jedis jedis = getJedis();
  try {
    return jedis.ltrim(keyToBytes(key), start, end);
  }
  finally {close(jedis);}
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public void lTrim(byte[] key, long start, long end) {
  Assert.notNull(key, "Key must not be null!");
  try {
    if (isPipelined()) {
      pipeline(connection.newStatusResult(connection.getRequiredPipeline().ltrim(key, start, end)));
      return;
    }
    if (isQueueing()) {
      transaction(connection.newStatusResult(connection.getRequiredTransaction().ltrim(key, start, end)));
      return;
    }
    connection.getJedis().ltrim(key, start, end);
  } catch (Exception ex) {
    throw convertJedisAccessException(ex);
  }
}

代码示例来源:origin: io.leopard/leopard-redis

@Override
  public Object execute(Jedis jedis) {
    return jedis.ltrim(key, start, end);
  }
});

代码示例来源:origin: tangyanbo/springmore

@Override
  public void action(Jedis jedis) {
    jedis.ltrim(key, 0, size - 1);
  }
});

代码示例来源:origin: tangyanbo/springmore

@Override
  public void action(Jedis jedis) {
    jedis.ltrim(key, start, end);
  }
});

代码示例来源:origin: yrain/smart-cache

@Override
  String doInJedis(Jedis jedis) {
    return jedis.ltrim(key, start, end);
  }
});

代码示例来源:origin: yrain/smart-cache

@Override
  String doInJedis(Jedis jedis) {
    return jedis.ltrim(key, start, end);
  }
});

代码示例来源:origin: apache/servicemix-bundles

@Override
 public String execute(Jedis connection) {
  return connection.ltrim(key, start, stop);
 }
}.runBinary(key);

代码示例来源:origin: io.leopard/leopard-redis

@Override
public String ltrim(String key, long start, long end) {
  return jedis.ltrim(key, start, end);
}

代码示例来源:origin: com.netflix.spinnaker.kork/kork-jedis

@Override
public String ltrim(String key, long start, long end) {
 String command = "ltrim";
 return instrumented(command, () -> delegated.ltrim(key, start, end));
}

代码示例来源:origin: com.netflix.spinnaker.kork/kork-jedis

@Override
public String ltrim(byte[] key, long start, long end) {
 String command = "ltrim";
 return instrumented(command, () -> delegated.ltrim(key, start, end));
}

代码示例来源:origin: com.github.sogyf/goja-mvt

@Override
  public Boolean action(Jedis jedis) {
    return "OK".equalsIgnoreCase(jedis.ltrim(SafeEncoder.encode(key), start, end));
  }
});

代码示例来源:origin: apache/servicemix-bundles

@Override
public String ltrim(final byte[] key, final long start, final long stop) {
 Jedis j = getShard(key);
 return j.ltrim(key, start, stop);
}

代码示例来源:origin: com.netflix.conductor/conductor-redis-persistence

@Override
public String ltrim(String key, long start, long end) {
 Jedis jedis = null;
  try {
   jedis = jedisPool.getResource();
   return jedis.ltrim(key, start, end);
  } finally {
   if (jedis != null)
    jedis.close();
  }
}

代码示例来源:origin: xiancloud/xian

@Override
public void execute(UnitRequest msg, Handler<UnitResponse> handler) {
  String key = msg.get("key");
  CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
  try {
    Redis.call(cacheConfigBean, (jedis) -> jedis.ltrim(key, 1, 0));
    handler.handle(UnitResponse.createSuccess());
  } catch (Exception e) {
    handler.handle(UnitResponse.createException(e));
  }
}

相关文章

Jedis类方法