diff --git a/examples/strings_example.cc b/examples/strings_example.cc index c4aa2570f1..ebef9e8c93 100644 --- a/examples/strings_example.cc +++ b/examples/strings_example.cc @@ -49,5 +49,16 @@ int main() { s = db.Compact(); printf("Compact return: %s\n", s.ToString().c_str()); + s = db.Setex("TEST_KEY", "TEST_VALUE", 1); + printf("Setex return: %s\n", s.ToString().c_str()); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + s = db.Get("TEST_KEY", &value); + printf("Get return: %s, value: %s\n", s.ToString().c_str(), value.c_str()); + + s = db.Set("TEST_KEY", "TEST_VALUE"); + int32_t len = 0; + s = db.Strlen("TEST_KEY", &len); + printf("Strlen return: %s, strlen: %d\n", s.ToString().c_str(), len); + return 0; } diff --git a/include/blackwidow/blackwidow.h b/include/blackwidow/blackwidow.h index 920a1a640b..58e92f3650 100644 --- a/include/blackwidow/blackwidow.h +++ b/include/blackwidow/blackwidow.h @@ -33,6 +33,8 @@ class BlackWidow { Status Get(const Slice& key, std::string* value); Status Setnx(const Slice& key, const Slice& value, int32_t* ret); Status Append(const Slice& key, const Slice& value, int32_t* ret); + Status Setex(const Slice& key, const Slice& value, int32_t ttl); + Status Strlen(const Slice& key, int32_t* len); // Keys Commands Status Expire(const Slice& key, int32_t ttl); diff --git a/src/blackwidow.cc b/src/blackwidow.cc index 2d070637ff..78614ae057 100644 --- a/src/blackwidow.cc +++ b/src/blackwidow.cc @@ -57,4 +57,12 @@ Status BlackWidow::Expire(const Slice& key, int32_t ttl) { return strings_db_->Expire(key, ttl); } +Status BlackWidow::Setex(const Slice& key, const Slice& value, int32_t ttl) { + return strings_db_->Setex(key, value, ttl); +} + +Status BlackWidow::Strlen(const Slice& key, int32_t* len) { + return strings_db_->Strlen(key, len); +} + } // namespace blackwidow diff --git a/src/redis_strings.cc b/src/redis_strings.cc index 074e81df10..281d7c7c4b 100644 --- a/src/redis_strings.cc +++ b/src/redis_strings.cc @@ -114,4 +114,24 @@ Status RedisStrings::CompactRange(const rocksdb::Slice* begin, return db_->CompactRange(default_compact_range_options_, begin, end); } +Status RedisStrings::Setex(const Slice& key, const Slice& value, int32_t ttl) { + //the ttl argument must greater than zero, to be compatible with redis + assert(ttl > 0); + InternalStringsValue internal_value(value); + internal_value.SetRelativeTimestamp(ttl); + ScopeRecordLock l(lock_mgr_, key); + return db_->Put(default_write_options_, key, internal_value.Encode()); +} + +Status RedisStrings::Strlen(const Slice& key, int32_t *len) { + std::string value; + Status s = Get(key, &value); + if (s.ok()) { + *len = value.size(); + } else { + *len = 0; + } + return s; +} + } // namespace blackwidow diff --git a/src/redis_strings.h b/src/redis_strings.h index 5031832a44..22cd8db6c7 100644 --- a/src/redis_strings.h +++ b/src/redis_strings.h @@ -23,6 +23,8 @@ class RedisStrings : public Redis { Status Get(const Slice& key, std::string* value); Status Setnx(const Slice& key, const Slice& value, int32_t* ret); Status Append(const Slice& key, const Slice& value, int32_t* ret); + Status Setex(const Slice& key, const Slice& value, int32_t ttl); + Status Strlen(const Slice& key, int32_t *len); // Common Commands virtual Status Open(const rocksdb::Options& options,