前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊claudb的keys command

聊聊claudb的keys command

原创
作者头像
code4it
修改2020-08-26 09:57:04
5780
修改2020-08-26 09:57:04
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下claudb的keys command

KeysCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/KeysCommand.java

代码语言:javascript
复制
@ReadOnly
@Command("keys")
@ParamLength(1)
public class KeysCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    GlobPattern pattern = createPattern(request.getParam(0));
    ImmutableSet<SafeString> keys = db.entrySet()
        .filter(matchPattern(pattern))
        .filter(filterExpired(Instant.now()).negate())
        .map(Tuple2::get1)
        .map(DatabaseKey::getValue);
    return convert(keys);
  }
​
  private GlobPattern createPattern(SafeString param) {
    return new GlobPattern(param.toString());
  }
​
  private Matcher1<Tuple2<DatabaseKey, DatabaseValue>> filterExpired(Instant now) {
    return entry -> entry.get2().isExpired(now);
  }
​
  private Matcher1<Tuple2<DatabaseKey, DatabaseValue>> matchPattern(GlobPattern pattern) {
    return entry -> pattern.match(entry.get1().toString());
  }
}
  • KeysCommand实现了DBCommand接口,其execute方法先通过createPattern创建GlobPattern,之后遍历db.entrySet(),过滤出matchPattern的,再过滤出非expired的,最后返回

DeleteCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/DeleteCommand.java

代码语言:javascript
复制
@Command("del")
@ParamLength(1)
public class DeleteCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    int removed = 0;
    for (SafeString key : request.getParams()) {
      DatabaseValue value = db.remove(safeKey(key));
      if (value != null) {
        removed += 1;
      }
    }
    return integer(removed);
  }
}
  • DeleteCommand实现了DBCommand接口,其execute方法遍历request.getParams()执行db.remove(safeKey(key)),最后返回integer(removed)

ExistsCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/ExistsCommand.java

代码语言:javascript
复制
@ReadOnly
@Command("exists")
@ParamLength(1)
public class ExistsCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    DatabaseValue value = db.get(safeKey(request.getParam(0)));
    return integer(value != null ? !value.isExpired(Instant.now()) : false);
  }
}
  • ExistsCommand实现了DBCommand接口,其execute方法先通过db.get(safeKey(request.getParam(0)))获取DatabaseValue,之后判断是否null,非null的话再判断是否expired,非expired的返回true

TypeCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TypeCommand.java

代码语言:javascript
复制
@ReadOnly
@Command("type")
@ParamLength(1)
public class TypeCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    DatabaseValue value = db.get(safeKey(request.getParam(0)));
    if (value != null) {
      return status(value.getType().text());
    } else {
      return status(DataType.NONE.text());
    }
  }
}
  • TypeCommand实现了DBCommand接口,其execute方法先通过db.get(safeKey(request.getParam(0)))获取DatabaseValue,若不为null则返回status(value.getType().text()),否则返回status(DataType.NONE.text())

RenameCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/RenameCommand.java

代码语言:javascript
复制
@Command("rename")
@ParamLength(2)
public class RenameCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    if (db.rename(safeKey(request.getParam(0)), safeKey(request.getParam(1)))) {
      return responseOk();
    } else {
      return error("ERR no such key");
    }
  }
}
  • RenameCommand实现了DBCommand接口,其execute方法执行db.rename

ExpireCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/ExpireCommand.java

代码语言:javascript
复制
@Command("expire")
@ParamLength(2)
public class ExpireCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    try {
      DatabaseValue value = db.get(safeKey(request.getParam(0)));
      if (value != null) {
        db.put(safeKey(request.getParam(0)), value.expiredAt(parsetTtl(request.getParam(1))));
      }
      return integer(value != null);
    } catch (NumberFormatException e) {
      return error("ERR value is not an integer or out of range");
    }
  }
​
  private int parsetTtl(SafeString param) {
    return Integer.parseInt(param.toString());
  }
}
  • ExpireCommand实现了DBCommand接口,其execute方法先通过db.get(safeKey(request.getParam(0)))获取DatabaseValue,若不为null则执行value.expiredAt(parsetTtl(request.getParam(1))),然后put进去

PersistCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/PersistCommand.java

代码语言:javascript
复制
@Command("persist")
@ParamLength(1)
public class PersistCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    DatabaseValue value = db.get(safeKey(request.getParam(0)));
    if (value != null) {
      db.put(safeKey(request.getParam(0)), value.noExpire());
    }
    return integer(value != null);
  }
}
  • PersistCommand方法实现了DBCommand接口,其execute方法先通过db.get(safeKey(request.getParam(0)))获取DatabaseValue,若不为null则执行db.put(safeKey(request.getParam(0)), value.noExpire())

TimeToLiveCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TimeToLiveCommand.java

代码语言:javascript
复制
public abstract class TimeToLiveCommand implements DBCommand {
​
  @Override
  public RedisToken execute(Database db, Request request) {
    DatabaseValue value = db.get(safeKey(request.getParam(0)));
    if (value != null) {
      return keyExists(value);
    } else {
      return notExists();
    }
  }
​
  protected abstract int timeToLive(DatabaseValue value, Instant now);
​
  private RedisToken keyExists(DatabaseValue value) {
    if (value.getExpiredAt() != null) {
      return hasExpiredAt(value);
    } else {
      return integer(-1);
    }
  }
​
  private RedisToken hasExpiredAt(DatabaseValue value) {
    Instant now = Instant.now();
    if (!value.isExpired(now)) {
      return integer(timeToLive(value, now));
    } else {
      return notExists();
    }
  }
​
  private RedisToken notExists() {
    return integer(-2);
  }
}
  • TimeToLiveCommand声明实现DBCommand接口,其execute接口先通过db.get(safeKey(request.getParam(0)))获取DatabaseValue,若为null则返回notExists,若不为null则执行keyExists;keyExists方法在value.getExpiredAt()不为null时执行hasExpiredAt方法;hasExpiredAt方法在value.isExpired(now)为false时返回integer(timeToLive(value, now));timeToLive方法为抽象方法,需要子类实现

TimeToLiveMillisCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TimeToLiveMillisCommand.java

代码语言:javascript
复制
@Command("pttl")
@ParamLength(1)
public class TimeToLiveMillisCommand extends TimeToLiveCommand {
​
  @Override
  protected int timeToLive(DatabaseValue value, Instant now) {
    return (int) value.timeToLiveMillis(now);
  }
}
  • TimeToLiveMillisCommand实现了TimeToLiveCommand接口,其timeToLive方法返回value.timeToLiveMillis(now)

TimeToLiveSecondsCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TimeToLiveSecondsCommand.java

代码语言:javascript
复制
@Command("ttl")
@ParamLength(1)
public class TimeToLiveSecondsCommand extends TimeToLiveCommand {
​
  @Override
  protected int timeToLive(DatabaseValue value, Instant now) {
    return value.timeToLiveSeconds(now);
  }
}
  • TimeToLiveSecondsCommand实现了TimeToLiveCommand接口,其timeToLive方法返回value.timeToLiveSeconds(now)

小结

claudb keys相关的command有KeysCommand、DeleteCommand、ExistsCommand、TypeCommand、RenameCommand、ExpireCommand、PersistCommand、TimeToLiveMillisCommand、TimeToLiveSecondsCommand

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • KeysCommand
  • DeleteCommand
  • ExistsCommand
  • TypeCommand
  • RenameCommand
  • ExpireCommand
  • PersistCommand
  • TimeToLiveCommand
    • TimeToLiveMillisCommand
    • TimeToLiveSecondsCommand
    • 小结
    • doc
    相关产品与服务
    云数据库 Redis®
    腾讯云数据库 Redis®(TencentDB for Redis®)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档