If we want to call Redis commands directly into the program, we can use LuaScript, which is located in the StackExchange.Redis library. Using LuaScript, we can execute several Redis commands in one request.
Consider a scenario in which the number of times an API or method is called is important to you, and if it is too much, do not run it or block the user (in this example, the purpose is to examine the Redis commands). To do this, we can use the INCR Radius method, which returns a number each time it is executed, which indicates how many times a key has been called. And then, if the number of requests is more than a certain amount, the value of 1- will be returned and the relevant key will be deleted, or its expiration time will be updated, or ...
To do this, you must first install the StackExchange.Redis package. Then, for use, we can write the required command ourselves and send it to Radis.

public async Task<int> Increment(string key, int maximum)
{
    string script = @"local count 
                      count = tonumber(redis.call('INCR', @key))
                      if(count > tonumber(@maximum))
                      then
                         redis.call('DEL', @key) 
                         return -1
                      else 
                         return count
                      end";
    LuaScript incrementScript = LuaScript.Prepare(script);

    var result = await _database
        .ScriptEvaluateAsync(incrementScript, new
        {
            key = new RedisKey(key),
            maximum
        });

    return (int)result;
}

In the script variable, we have written the command related to the radius. First, the INCR method is called and the returned value is placed in the local variable count. Otherwise the same amount returned will be returned by the INCR method. If the key you want is not in the radius, it is then stored in the radius and then the value 1 is returned.
Using the ScriptEvaluateAsync method, you can execute the written commands, and in the first parameter you have to send the written script, and in the second parameter you have to send the required parameters of the script, in this example, we have sent a key and a maximum. The return value of this method is a RedisResult that you must cast to your desired result.
To connect to Radis, we used the IConnectionMultiplexer, which was prototyped in the builder of the RedisRepository class.

private readonly IDatabase _database;
public RedisRepostitory(IConnectionMultiplexer connectionMultiplexer)
{
    _database = connectionMultiplexer.GetDatabase();
}

Then we need to register the IConnectionMultiplexer to DI in Startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSingleton<IRedisRepostitory, RedisRepostitory>();
    services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(Configuration["Redis:Host"]));
}

You can download the codes of this article from GitHub.
;)

Powered by Froala Editor

Comments