KV().Get

Contents

KV().Get#

static inline std::string YR::KVManager::Get(const std::string &key, int timeout = DEFAULT_GET_TIMEOUT_SEC)#

Retrieves a value associated with a specified key, similar to Redis’s GET command.

This function fetches the value stored under the specified key. If the key does not exist, an exception is thrown.

Parameters:
  • key – The key associated with the value to retrieve. The key must not be empty.

  • timeout – The maximum time in seconds to wait for the value to be retrieved. If the value is not available within this time, an exception is thrown. The default value is 300 seconds. The timeout can be set to -1 to wait indefinitely.

Throws:

Exception – Thrown in the following cases:

  • 1001: Invalid input parameters (e.g., empty keys or invalid characters).

  • 4005: Get operation failed (e.g., key not found or timeout exceeded).

  • 4201: RocksDB error (e.g., disk issues).

  • 4202: Shared memory limit exceeded.

  • 4203: Disk operation failed (e.g., permission issues).

  • 4204: Disk space full.

  • 1000, 1001, 1002: Internal communication errors.

Returns:

std::string The value associated with the specified key.

static inline std::vector<std::string> YR::KVManager::Get(const std::vector<std::string> &keys, int timeout = DEFAULT_GET_TIMEOUT_SEC, bool allowPartial = false)#

Retrieves multiple values associated with specified keys, similar to Redis’s MGET command.

This function fetches values stored under the specified keys. If any key does not exist or the operation times out, an exception is thrown unless allowPartial is set to true.

int main() {
    YR::Config conf;
    conf.mode = Config::Mode::CLUSTER_MODE;
    YR::Init(conf);
    std::string value1{ "result" };
    std::string value2{ "result1" };
    YR::KV().Set("key", value1.c_str());
    std::string returnVal = YR::KV().Get("key"); // Retrieves "result"

    std::vector<std::string> keys{ "key1", "key2" };
    YR::KV().Set(keys[0], value1);
    YR::KV().Set(keys[1], value2);

    std::vector<std::string> returnVal = YR::KV().Get(keys); // Retrieves { "result", "result1" }
    return 0;
}

Note

  • When allowPartial = false: All keys must be retrieved successfully; otherwise, an exception is thrown.

  • When allowPartial = true: Returns a vector of results where failed keys have empty strings. If all keys fail, an exception is thrown.

Parameters:
  • keys – A list of keys to retrieve values for. The maximum number of keys is 10000.

  • timeout – The maximum time in seconds to wait for the values to be retrieved. The default value is 300 seconds. Setting to -1 waits indefinitely.

  • allowPartial – Whether to allow partial success results. If true, the function returns a vector of results where failed keys have empty strings. If false, the function throws an exception if any key fails. Default is false.

Throws:

Exception – Thrown in the following cases:

  • 1001: Invalid input parameters (e.g., empty keys or invalid characters).

  • 4005: Get operation failed (e.g., key not found or timeout exceeded).

  • 4201: RocksDB error (e.g., disk issues).

  • 4202: Shared memory limit exceeded.

  • 4203: Disk operation failed (e.g., permission issues).

  • 4204: Disk space full.

  • 1000, 1001, 1002: Internal communication errors.

Returns:

std::vector<std::string> A vector containing the retrieved values. The order of values corresponds to the order of keys.