Redis C# - Using Incr value in a transaction

There is a fundamental feature of redis MULTI/EXEC units that means you cannot get results during the operation. As such, there are two common ways of doing what you ask:

  1. Use Lua (ScriptEvaluate[Async]); a Lua script executes on the server start-to-end, is efficient, and avoids all problems associated with round-trip time (latency or bandwidth) or competition from other connections
  2. Use an optimistic loop that reads the current value, then creates a transaction in SE-Redis that adds a constraint that the value you just read is the same, and execute the side-effects inside the transaction; the library will co-ordinate the necessary WATCH etc machinery to make this robust, but if the constraint condition turns out to be invalid (i.e. the library returns false), you need to redo everything from the start

Frankly, these days I would always guide people to option 1; option 2 is only "appealing" (and I use that term quite incorrectly) if server-side Lua scripting is not available.

I'm not at a PC, but I'm guessing the script would look something like:

local id = redis.call("incr", KEYS[1])
redis.call("hset", KEYS[2], tostring(id), ARGV[1])
return id

Comments

  1. Colin

    • 2020/7/7

    Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such 

  2. Ellis

    • 2019/7/13

    In order to use Redis with C you need a C Redis client. In following sections, we demonstrate the use of hiredis, a simple C client for Redis. Additional C clients for Redis can be found under the C section of the Redis Clients page. Installing hiredis. Download the latest hiredis release from the GitHub repository.

  3. Aden

    • 2021/5/24

    In order to use Redis with C you need a C Redis client. In following sections, we demonstrate the use of hiredis, a simple C client for Redis.

  4. Aries

    • 2021/6/29

    Redis with C. In order to use Redis with C you need a C Redis client. In following sections, we demonstrate the use of hiredis, a simple C client for Redis. Additional C clients for Redis can be found under the C section of the Redis Clients page. Installing hiredis. Download the latest hiredis release from the GitHub repository.

  5. Maximiliano

    • 2020/3/31

    Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, 

  6. Ross

    • 2019/6/3

    Redis Cluster C++ Client, based on hiredis, support password and standalone, it's easy to make and use, not depends on C++11 or later. redis-cpp redis-cpp is a library in C++17 for executing Redis commands with support of the pipelines and publish / subscribe pattern redis-plus-plus

  7. Avery

    • 2015/1/25

    . Commands such as GEOADD, GEODIST, GEORADIUS, and GEORADIUSBYMEMBER to store, process, and analyze geospatial data in real-time make geospatial easy and fast with Redis.

  8. Martini

    • 2016/11/21

    Command ZINCRBY (43) increments by 1 the score of date in the key status. As a result the database contains keys (install, upgrade, remove …) and associated lists of dates sorted by score. Next loop (97) calls the function write_csv with first 10 keys. As a result status.csv files are created in the current directory with the (date;score) pairs.

  9. Callen

    • 2021/5/13

    that can be used to store and retrieve data in your applications. Redis Cache is an open source, high-speed, NoSQL database. It's fast, and it runs entirely in memory with negligible performance overhead when reading and writing data.

  10. Valentini

    • 2015/6/2

    In order to use Redis in .NET, you need a .NET Redis client. This article shows how to use StackExchange.Redis, a general purpose Redis client. More .NET Redis clients can be found in the C# section of the Redis Clients page. Installing StackExchange.Redis. There are several ways to install this package including: With the .NET CLI:

  11. Nathanael

    • 2016/9/11

    , or using the MODULE LOAD command. Modules are designed in order to be loaded into different versions of Redis, so a given module does not need to be designed, or recompiled, in order to run with a specific version of Redis.

  12. Giovanni

    • 2016/12/18

    Redis is written in ANSI C and works in most POSIX systems like Linux, *BSD, and OS X, without external dependencies. Linux and OS X are the two operating systems where Redis is developed and tested the most, and we recommend using Linux for deployment. Redis may work in Solaris-derived systems like SmartOS, but the support is best effort

  13. Marvin

    • 2015/10/3

    Minimalistic C client for Redis >= 1.2. Contribute to redis/hiredis development by creating an account on GitHub.

  14. Allan

    • 2017/11/4

    Redis Tutorial. Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server, since the keys can contain strings, hashes, lists, sets and sorted sets. Redis is written in C. This tutorial provides good understanding on Redis concepts, needed to create and deploy a highly scalable and

  15. Marini

    • 2017/11/27

    Helloworld module -- A few examples of the Redis Modules API in the form * of commands showing how to accomplish reply = RedisModule_Call(ctx,"INCR","c!

  16. Zayd

    • 2017/9/24

    Hiredis is a very comprehensive C language version of redis interface library, supporting all commands, pipes and scripts. Huawei cloud 

  17. Martino

    • 2016/4/13

    Connecting to a Redis instance from a Compute Engine VM using telnet redis-benchmark -c 100 -n 100000 -d 1024 -r 100000 -t PING,SET,GET,INCR,LPUSH,RPUSH 

Comments are closed.

Recent Posts