Git Product home page Git Product logo

redis_utils's Introduction

redis_utils

Cohesive helpers built on top of redis-rs for:

  • async transactions
  • geting and seting json values

Async Transactions

A macro that helps you set up a safe async redis transaction.

Takes:

  • A connection
  • The name of a pipeline which it configures in atomic-mode.
  • A set of keys to WATCH
  • The body of the transaction that can get those keys, use the pipeline (for side effects) and if those keys change (and the EXEC component of the atomic pipeline fails), then the body will be re-executed.
  • Allows for safe early returns (aborted transactions) with typed values, all keys will be un-watched during an early return.
tx!(&mut con, pipe, &["key1"], {
  let mut value: u8 = con.get("key1").await?;
  value = value + 1;
  
  Ok(pipe.set("key1", value))
});

Aborting a tx

tx!(&mut con, pipe, &["key1"], {
  let mut value: u8 = con.get("key1").await?;
  value = value + 1;
  
  if value == 69 {
    return Err(Abort(BadNumberFound));
  }
  
  Ok(pipe.set("key1", value))
});

Handling return values

let tx: Result<u8, TxError<NumberError> > = tx!(&mut con, pipe, &["key1"], {
  let mut value: u8 = con.get("key1").await?;
  value = value + 1;
  
  if value == 69 {
    return Err(Abort(BadNumberFound));
  }
  
  Ok(pipe.set("key1", value))
});
  • The Ok(T) of tx is the type that's handed to pipe.set() for redis-rs's type inference.
  • TxError allows you to return any type in TxError::Abort for custom type handling.
  • If the transaction fails due to an underlying redis error or serde tx will reflect this in the associated TxError::DbError or TxError::Serialization.

JSON helpers

Using the helpers from TODO allow you to turn this:

let json_string: String = con.get(key).await?;
let value: Type = serde_json::from_str(&json_string).unwrap;
let value: Type = con.json_get(key).await.unwrap();

redis_utils's People

Contributors

parth avatar smailbarkouch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.