1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
use crate::{
commands,
error::RedisError,
interfaces::{ClientLike, RedisResult},
types::{Expiration, ExpireOptions, FromRedis, MultipleKeys, RedisKey, RedisMap, RedisValue, SetOptions},
};
use fred_macros::rm_send_if;
use futures::Future;
use std::convert::TryInto;
/// Functions that implement the generic [keys](https://redis.io/commands#generic) interface.
#[rm_send_if(feature = "glommio")]
pub trait KeysInterface: ClientLike + Sized {
/// Marks the given keys to be watched for conditional execution of a transaction.
///
/// <https://redis.io/commands/watch>
fn watch<K>(&self, keys: K) -> impl Future<Output = RedisResult<()>> + Send
where
K: Into<MultipleKeys> + Send,
{
async move {
into!(keys);
commands::keys::watch(self, keys).await
}
}
/// Flushes all the previously watched keys for a transaction.
///
/// <https://redis.io/commands/unwatch>
fn unwatch(&self) -> impl Future<Output = RedisResult<()>> + Send {
async move { commands::keys::unwatch(self).await }
}
/// Return a random key from the currently selected database.
///
/// <https://redis.io/commands/randomkey>
fn randomkey<R>(&self) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
{
async move { commands::keys::randomkey(self).await?.convert() }
}
/// This command copies the value stored at the source key to the destination key.
///
/// <https://redis.io/commands/copy>
fn copy<R, S, D>(
&self,
source: S,
destination: D,
db: Option<u8>,
replace: bool,
) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
S: Into<RedisKey> + Send,
D: Into<RedisKey> + Send,
{
async move {
into!(source, destination);
commands::keys::copy(self, source, destination, db, replace)
.await?
.convert()
}
}
/// Serialize the value stored at `key` in a Redis-specific format and return it as bulk string.
///
/// <https://redis.io/commands/dump>
fn dump<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::dump(self, key).await?.convert()
}
}
/// Create a key associated with a value that is obtained by deserializing the provided serialized value
///
/// <https://redis.io/commands/restore>
fn restore<R, K>(
&self,
key: K,
ttl: i64,
serialized: RedisValue,
replace: bool,
absttl: bool,
idletime: Option<i64>,
frequency: Option<i64>,
) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::restore(self, key, ttl, serialized, replace, absttl, idletime, frequency)
.await?
.convert()
}
}
/// Set a value with optional NX|XX, EX|PX|EXAT|PXAT|KEEPTTL, and GET arguments.
///
/// Note: the `get` flag was added in 6.2.0. Setting it as `false` works with Redis versions <=6.2.0.
///
/// <https://redis.io/commands/set>
fn set<R, K, V>(
&self,
key: K,
value: V,
expire: Option<Expiration>,
options: Option<SetOptions>,
get: bool,
) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
V: TryInto<RedisValue> + Send,
V::Error: Into<RedisError> + Send,
{
async move {
into!(key);
try_into!(value);
commands::keys::set(self, key, value, expire, options, get)
.await?
.convert()
}
}
/// Read a value from the server.
///
/// <https://redis.io/commands/get>
fn get<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::get(self, key).await?.convert()
}
}
/// Returns the substring of the string value stored at `key` with offsets `start` and `end` (both inclusive).
///
/// Note: Command formerly called SUBSTR in Redis verison <=2.0.
///
/// <https://redis.io/commands/getrange>
fn getrange<R, K>(&self, key: K, start: usize, end: usize) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::getrange(self, key, start, end).await?.convert()
}
}
/// Overwrites part of the string stored at `key`, starting at the specified `offset`, for the entire length of
/// `value`.
///
/// <https://redis.io/commands/setrange>
fn setrange<R, K, V>(&self, key: K, offset: u32, value: V) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
V: TryInto<RedisValue> + Send,
V::Error: Into<RedisError> + Send,
{
async move {
into!(key);
try_into!(value);
commands::keys::setrange(self, key, offset, value).await?.convert()
}
}
/// Atomically sets `key` to `value` and returns the old value stored at `key`.
///
/// Returns an error if `key` does not hold string value. Returns nil if `key` does not exist.
///
/// <https://redis.io/commands/getset>
fn getset<R, K, V>(&self, key: K, value: V) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
V: TryInto<RedisValue> + Send,
V::Error: Into<RedisError> + Send,
{
async move {
into!(key);
try_into!(value);
commands::keys::getset(self, key, value).await?.convert()
}
}
/// Get the value of key and delete the key. This command is similar to GET, except for the fact that it also
/// deletes the key on success (if and only if the key's value type is a string).
///
/// <https://redis.io/commands/getdel>
fn getdel<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::getdel(self, key).await?.convert()
}
}
/// Returns the length of the string value stored at key. An error is returned when key holds a non-string value.
///
/// <https://redis.io/commands/strlen>
fn strlen<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::strlen(self, key).await?.convert()
}
}
/// Removes the specified keys. A key is ignored if it does not exist.
///
/// Returns the number of keys removed.
///
/// <https://redis.io/commands/del>
fn del<R, K>(&self, keys: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<MultipleKeys> + Send,
{
async move {
into!(keys);
commands::keys::del(self, keys).await?.convert()
}
}
/// Unlinks the specified keys. A key is ignored if it does not exist
///
/// Returns the number of keys removed.
///
/// <https://redis.io/commands/del>
fn unlink<R, K>(&self, keys: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<MultipleKeys> + Send,
{
async move {
into!(keys);
commands::keys::unlink(self, keys).await?.convert()
}
}
/// Renames `source` key to `destination`.
///
/// Returns an error when `source` does not exist. If `destination` exists, it gets overwritten.
///
/// <https://redis.io/commands/rename>
fn rename<R, S, D>(&self, source: S, destination: D) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
S: Into<RedisKey> + Send,
D: Into<RedisKey> + Send,
{
async move {
into!(source);
into!(destination);
commands::keys::rename(self, source, destination).await?.convert()
}
}
/// Renames `source` key to `destination` if `destination` does not yet exist.
///
/// Returns an error when `source` does not exist.
///
/// <https://redis.io/commands/renamenx>
fn renamenx<R, S, D>(&self, source: S, destination: D) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
S: Into<RedisKey> + Send,
D: Into<RedisKey> + Send,
{
async move {
into!(source);
into!(destination);
commands::keys::renamenx(self, source, destination).await?.convert()
}
}
/// Append `value` to `key` if it's a string.
///
/// <https://redis.io/commands/append/>
fn append<R, K, V>(&self, key: K, value: V) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
V: TryInto<RedisValue> + Send,
V::Error: Into<RedisError> + Send,
{
async move {
into!(key);
try_into!(value);
commands::keys::append(self, key, value).await?.convert()
}
}
/// Returns the values of all specified keys. For every key that does not hold a string value or does not exist, the
/// special value nil is returned.
///
/// <https://redis.io/commands/mget>
fn mget<R, K>(&self, keys: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<MultipleKeys> + Send,
{
async move {
into!(keys);
commands::keys::mget(self, keys).await?.convert()
}
}
/// Sets the given keys to their respective values.
///
/// <https://redis.io/commands/mset>
fn mset<V>(&self, values: V) -> impl Future<Output = RedisResult<()>> + Send
where
V: TryInto<RedisMap> + Send,
V::Error: Into<RedisError> + Send,
{
async move {
try_into!(values);
commands::keys::mset(self, values).await?.convert()
}
}
/// Sets the given keys to their respective values. MSETNX will not perform any operation at all even if just a
/// single key already exists.
///
/// <https://redis.io/commands/msetnx>
fn msetnx<R, V>(&self, values: V) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
V: TryInto<RedisMap> + Send,
V::Error: Into<RedisError> + Send,
{
async move {
try_into!(values);
commands::keys::msetnx(self, values).await?.convert()
}
}
/// Increments the number stored at `key` by one. If the key does not exist, it is set to 0 before performing the
/// operation.
///
/// Returns an error if the value at key is of the wrong type.
///
/// <https://redis.io/commands/incr>
fn incr<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::incr(self, key).await?.convert()
}
}
/// Increments the number stored at `key` by `val`. If the key does not exist, it is set to 0 before performing the
/// operation.
///
/// Returns an error if the value at key is of the wrong type.
///
/// <https://redis.io/commands/incrby>
fn incr_by<R, K>(&self, key: K, val: i64) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::incr_by(self, key, val).await?.convert()
}
}
/// Increment the string representing a floating point number stored at key by `val`. If the key does not exist, it
/// is set to 0 before performing the operation.
///
/// Returns an error if key value is the wrong type or if the current value cannot be parsed as a floating point
/// value.
///
/// <https://redis.io/commands/incrbyfloat>
fn incr_by_float<R, K>(&self, key: K, val: f64) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::incr_by_float(self, key, val).await?.convert()
}
}
/// Decrements the number stored at `key` by one. If the key does not exist, it is set to 0 before performing the
/// operation.
///
/// Returns an error if the key contains a value of the wrong type.
///
/// <https://redis.io/commands/decr>
fn decr<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::decr(self, key).await?.convert()
}
}
/// Decrements the number stored at `key` by `val`. If the key does not exist, it is set to 0 before performing the
/// operation.
///
/// Returns an error if the key contains a value of the wrong type.
///
/// <https://redis.io/commands/decrby>
fn decr_by<R, K>(&self, key: K, val: i64) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::decr_by(self, key, val).await?.convert()
}
}
/// Returns the remaining time to live of a key that has a timeout, in seconds.
///
/// <https://redis.io/commands/ttl>
fn ttl<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::ttl(self, key).await?.convert()
}
}
/// Returns the remaining time to live of a key that has a timeout, in milliseconds.
///
/// <https://redis.io/commands/pttl>
fn pttl<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::pttl(self, key).await?.convert()
}
}
/// Remove the existing timeout on a key, turning the key from volatile (a key with an expiration)
/// to persistent (a key that will never expire as no timeout is associated).
///
/// Returns a boolean value describing whether the timeout was removed.
///
/// <https://redis.io/commands/persist>
fn persist<R, K>(&self, key: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::persist(self, key).await?.convert()
}
}
/// Set a timeout on key. After the timeout has expired, the key will be automatically deleted.
///
/// <https://redis.io/commands/expire>
fn expire<R, K>(&self, key: K, seconds: i64) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::expire(self, key, seconds).await?.convert()
}
}
/// Set a timeout on a key based on a UNIX timestamp.
///
/// <https://redis.io/commands/expireat>
fn expire_at<R, K>(&self, key: K, timestamp: i64) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::expire_at(self, key, timestamp).await?.convert()
}
}
/// This command works exactly like EXPIRE but the time to live of the key is specified in milliseconds instead of
/// seconds.
///
/// <https://redis.io/docs/latest/commands/pexpire/>
fn pexpire<R, K>(
&self,
key: K,
milliseconds: i64,
options: Option<ExpireOptions>,
) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::pexpire(self, key, milliseconds, options)
.await?
.convert()
}
}
/// PEXPIREAT has the same effect and semantic as EXPIREAT, but the Unix time at which the key will expire is
/// specified in milliseconds instead of seconds.
///
/// <https://redis.io/docs/latest/commands/pexpireat/>
fn pexpire_at<R, K>(
&self,
key: K,
timestamp: i64,
options: Option<ExpireOptions>,
) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<RedisKey> + Send,
{
async move {
into!(key);
commands::keys::pexpire_at(self, key, timestamp, options)
.await?
.convert()
}
}
/// Returns number of keys that exist from the `keys` arguments.
///
/// <https://redis.io/commands/exists>
fn exists<R, K>(&self, keys: K) -> impl Future<Output = RedisResult<R>> + Send
where
R: FromRedis,
K: Into<MultipleKeys> + Send,
{
async move {
into!(keys);
commands::keys::exists(self, keys).await?.convert()
}
}
/// Runs the longest common subsequence algorithm on two keys.
///
/// <https://redis.io/commands/lcs/>
fn lcs<R, K1, K2>(
&self,
key1: K1,
key2: K2,
len: bool,
idx: bool,
minmatchlen: Option<i64>,
withmatchlen: bool,
) -> impl Future<Output = Result<R, RedisError>> + Send
where
R: FromRedis,
K1: Into<RedisKey> + Send,
K2: Into<RedisKey> + Send,
{
async move {
into!(key1, key2);
commands::keys::lcs(self, key1, key2, len, idx, minmatchlen, withmatchlen)
.await?
.convert()
}
}
}