public static void Main(string[] args) { // Run MONITOR command on redis-cli to display the commands that are being executed using (var redis = ConnectionMultiplexer.Connect("127.0.0.1")) { var redisDb = redis.GetDatabase(); var server = redis.GetServer("127.0.0.1:6379"); // Reset the key redisDb.KeyDelete("statistics"); // Converts the script to a single line in order to be loaded to Redis // Also does some StackExchange.Redis specific arguments replacement LuaScript prepared = LuaScript.Prepare(File.ReadAllText("calculate_stats.lua")); // Loads the script into the server and returns the SHA LoadedLuaScript loaded = prepared.Load(server); Console.WriteLine("Script SHA is " + BitConverter.ToString(loaded.Hash).Replace("-", String.Empty)); for (int i = 1; i <= 5; ++i) { loaded.Evaluate(redisDb, new { key = (RedisKey)"statistics", value = 10 * i }); } } }
/// <summary> /// /// </summary> /// <param name="redisEndpoint"></param> /// <param name="redisPassword"></param> /// <param name="redisSsl"></param> /// <param name="onException"></param> /// <param name="onThrottled"></param> /// <param name="connectionTimeoutInMilliseconds"></param> /// <param name="syncTimeoutInMilliseconds"></param> /// <param name="countThrottledRequests"></param> /// <param name="circuitBreaker"></param> /// <param name="clock"></param> /// <param name="connectToRedisFunc"></param> public LeakyBucketRateLimiter( string redisEndpoint, string redisPassword = null, bool redisSsl = false, Action <Exception> onException = null, Action <RateLimitingResult> onThrottled = null, int connectionTimeoutInMilliseconds = 2000, int syncTimeoutInMilliseconds = 1000, bool countThrottledRequests = false, ICircuitBreaker circuitBreaker = null, IClock clock = null, Func <Task <IConnectionMultiplexer> > connectToRedisFunc = null) : base(redisEndpoint, redisPassword, redisSsl, onException, onThrottled, connectionTimeoutInMilliseconds, syncTimeoutInMilliseconds, countThrottledRequests, circuitBreaker, clock, connectToRedisFunc) { var prepared = LuaScript.Prepare(_luaScript); _loadedLuaScript = prepared.Load( _redisConnection.GetServer( _redisConnection.GetDatabase().IdentifyEndpoint())); }
public RedisClientManager(string connectionString) { _connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString); var prepared = LuaScript.Prepare(LuaZPOPMIN); _loadedZpopminScript = prepared.Load(_connectionMultiplexer.GetServer(connectionString)); }
private void Init() { const string script = "local count = tonumber(@count) " + "local max = tonumber(@max) " + "local min = tonumber(@min) " + "local sum = tonumber(@sum) " + "local timestamp = tonumber(@timestamp) " + "local oldValues = redis.call('zrangebyscore', @metricKey, timestamp, timestamp) " + "if (next(oldValues) == nil) " + "then " + " redis.call('zadd', @metricKey, timestamp, timestamp .. ' ' .. count .. ' ' .. max .. ' ' .. min .. ' ' .. sum)" + "else " + " local s = oldValues[1] " + " local oldData = {} " + " for word in s:gmatch('%S+') do table.insert(oldData, word) end " + " local oldCount = tonumber(oldData[2]) " + " local oldMax = tonumber(oldData[3]) " + " local oldMin = tonumber(oldData[4]) " + " local oldSum = tonumber(oldData[5]) " + " redis.call('zremrangebyscore', @metricKey, timestamp, timestamp) " + " redis.call('zadd', @metricKey, timestamp, timestamp .. ' ' .. count + oldCount .. ' ' .. math.max(oldMax, max) .. ' ' .. math.min(oldMin, min) .. ' ' .. sum + oldSum) " + "end"; var server = connectionMultiplexer.GetServer(redisSettings.Host); var prepared = LuaScript.Prepare(script); loadedLuaScript = prepared.Load(server); }
public RedisTransaction(IRedisTransaction transaction, LoadedLuaScript zpopMinScript) { _transaction = transaction; _zpopMinScript = zpopMinScript; _notExistsChecks = new Dictionary <string, ConditionResult>(); _existsChecks = new Dictionary <string, ConditionResult>(); }
private async Task LoadScriptsAsync() { if (_scriptsLoaded) { return; } using (await _lock.LockAsync().AnyContext()) { if (_scriptsLoaded) { return; } var dequeueId = LuaScript.Prepare(DequeueIdScript); foreach (var endpoint in _options.ConnectionMultiplexer.GetEndPoints()) { var server = _options.ConnectionMultiplexer.GetServer(endpoint); if (server.IsReplica) { continue; } _dequeueId = await dequeueId.LoadAsync(server).AnyContext(); } _scriptsLoaded = true; } }
private async Task LoadScriptsAsync() { if (_scriptsLoaded) { return; } using (await _lock.LockAsync().AnyContext()) { if (_scriptsLoaded) { return; } var setIfLower = LuaScript.Prepare(SET_IF_LOWER); var setIfHigher = LuaScript.Prepare(SET_IF_HIGHER); var incrByAndExpire = LuaScript.Prepare(INCRBY_AND_EXPIRE); var delByWildcard = LuaScript.Prepare(DEL_BY_WILDCARD); foreach (var endpoint in _connectionMultiplexer.GetEndPoints()) { var server = _connectionMultiplexer.GetServer(endpoint); _setIfHigherScript = await setIfHigher.LoadAsync(server).AnyContext(); _setIfLowerScript = await setIfLower.LoadAsync(server).AnyContext(); _incrByAndExpireScript = await incrByAndExpire.LoadAsync(server).AnyContext(); _delByWildcardScript = await delByWildcard.LoadAsync(server).AnyContext(); } _scriptsLoaded = true; } }
public void Setup() { string script = "local count = tonumber(@count) " + "local max = tonumber(@max) " + "local min = tonumber(@min) " + "local sum = tonumber(@sum) " + "local timestamp = tonumber(@timestamp) " + "local oldValues = redis.call('zrangebyscore', @metricKey, timestamp, timestamp) " + "if (next(oldValues) == nil) " + "then " + " redis.call('zadd', @metricKey, timestamp, timestamp .. ' ' .. count .. ' ' .. max .. ' ' .. min .. ' ' .. sum)" + "else " + " local s = oldValues[1] " + " local oldData = {} " + " for word in s:gmatch('%S+') do table.insert(oldData, word) end " + " local oldCount = tonumber(oldData[2]) " + " local oldMax = tonumber(oldData[3]) " + " local oldMin = tonumber(oldData[4]) " + " local oldSum = tonumber(oldData[5]) " + " redis.call('zremrangebyscore', @metricKey, timestamp, timestamp) " + " redis.call('zadd', @metricKey, timestamp, timestamp .. ' ' .. count + oldCount .. ' ' .. math.max(oldMax, max) .. ' ' .. math.min(oldMin, min) .. ' ' .. sum + oldSum) " + "end"; conn = ConnectionMultiplexer.Connect("127.0.0.1:7001"); db = conn.GetDatabase(2); var server = conn.GetServer("127.0.0.1", 7001); var prepared = LuaScript.Prepare(script); loaded = prepared.Load(server); }
public void SetQuerry(string _queryText) { queryText = _queryText; prepared = LuaScript.Prepare(queryText); loaded = prepared.Load(REDISConnector.REDISConnector.GetRedis().GetServer(server)); }
/// <summary> /// Creates a new instance of RSMQ. /// </summary> public Rsmq(RsmqOptions options = null) { this._options = options ?? new RsmqOptions(); this._connection = new RedisConnectionPool($"{this._options.Host}:{this._options.Port}{(this._options.Options != null ? $",{this._options.Options.TrimStart(',')}" : "")}"); _receiveMessage = LuaScripts.ReceiveMessageScript.Load(this.RedisServer); _popMessage = LuaScripts.PopMessageScript.Load(this.RedisServer); _changeMessageVisibility = LuaScripts.ChangeMessageVisibilityScript.Load(this.RedisServer); }
/// <summary> /// Initializes a new instance of the <see cref="MemoryStore" /> class. /// </summary> /// <param name="database">The database.</param> /// <param name="deleteKeyPrefixScript">The delete key prefix script.</param> /// <exception cref="System.ArgumentNullException">database</exception> public MemoryStore(IDatabase database, LoadedLuaScript deleteKeyPrefixScript) { if (database == null) { throw new ArgumentNullException("database"); } _database = database; _deleteKeyPrefixScript = deleteKeyPrefixScript; }
private async Task CreateScriptsOnAllServers() { var luaScript = LuaScript.Prepare(Lua.PublishLatest); foreach (var endpoint in _connection.GetEndPoints()) { var hostname = GetServerHostnameAndPort(endpoint); var server = _connection.GetServer(hostname); _publishLatestScript = await luaScript.LoadAsync(server).ConfigureAwait(false); } }
/// <summary> /// Prepares the scripts. /// </summary> /// <param name="server">The server.</param> /// <exception cref="System.ArgumentNullException">server</exception> private void PrepareScripts(IServer server) { if (server == null) { throw new ArgumentNullException("server"); } const string deleteKeyPrefixScript = "local keys = redis.call('keys', ARGV[1]) for i=1,#keys,5000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return keys"; LuaScript deleteKeyPrefixPreparedScript = LuaScript.Prepare(deleteKeyPrefixScript); _deleteKeyPrefixScript = deleteKeyPrefixPreparedScript.Load(server); }
private LoadedLuaScript LoadScript(string script) { var preparedScript = LuaScript.Prepare(script); LoadedLuaScript loadedScript = null; // load script on all servers // note: only need to store single instance of loaded script as it is a wrapper around the original prepared script foreach (var server in _redis.Value.GetEndPoints().Select(x => _redis.Value.GetServer(x))) { loadedScript = preparedScript.Load(server); } return(loadedScript); }
internal Consumer(RingBuffer queue, ConsumerOptions options = null) { Queue = queue; if (options == null) { options = new ConsumerOptions(); } _options = options; _subscriptionPositionKey = Queue.DbKey.GetSubscriptionKey(_options.SubscriptionId); _script = LuaScript.Prepare(ScriptPreprocessor(File.ReadAllText("Scripts/consume.lua"))).Load(Queue.Server); }
public async Task EnsureLoaded(IConnectionMultiplexer redis) { if (LoadedLuaScript != null) { return; } var servers = redis.GetEndPoints() .Select(ep => redis.GetServer(ep)); foreach (var server in servers) { LoadedLuaScript = await LuaScript.LoadAsync(server, CommandFlags.HighPriority); } }
private void LoadScripts() { var setIfLower = LuaScript.Prepare(SET_IF_LOWER); var setIfHigher = LuaScript.Prepare(SET_IF_HIGHER); var incrByAndExpire = LuaScript.Prepare(INCRBY_AND_EXPIRE); var delByWildcard = LuaScript.Prepare(DEL_BY_WILDCARD); foreach (var endpoint in _connectionMultiplexer.GetEndPoints()) { _setIfHigherScript = setIfHigher.Load(_connectionMultiplexer.GetServer(endpoint)); _setIfLowerScript = setIfLower.Load(_connectionMultiplexer.GetServer(endpoint)); _incrByAndExpireScript = incrByAndExpire.Load(_connectionMultiplexer.GetServer(endpoint)); _delByWildcardScript = delByWildcard.Load(_connectionMultiplexer.GetServer(endpoint)); } }
public async Task <bool> CancelTask(string taskKey) { if (LoadedCancelTaskScript == null) { LoadedCancelTaskScript = await _taskQueue.Backend.LoadLuaScript(LuaScriptToCancelTask()); } var didCancel = await _taskQueue.Backend.RunLuaScript(LoadedCancelTaskScript, new [] { (RedisKey)ScheduledTasksOrderedSetKey, (RedisKey)ScheduledTasksMapKey, }, new [] { (RedisValue)taskKey }); return((bool)didCancel); }
private RedisResult Eval(ScriptType scriptType, RedisKey redisKey, RedisValue[] values = null, CommandFlags flags = CommandFlags.None) { if (!_scriptsLoaded) { lock (_lockObject) { if (!_scriptsLoaded) { LoadScripts(); _scriptsLoaded = true; } } } LoadedLuaScript script = null; if (!_luaScripts.TryGetValue(scriptType, out LuaScript luaScript) || (_canPreloadScripts && !_shaScripts.TryGetValue(scriptType, out script))) { Logger.LogCritical("Something is wrong with the Lua scripts. Seem to be not loaded."); _scriptsLoaded = false; throw new InvalidOperationException("Something is wrong with the Lua scripts. Seem to be not loaded."); } try { if (_canPreloadScripts && script != null) { return(_connection.Database.ScriptEvaluate(script.Hash, new[] { redisKey }, values, flags)); } else { return(_connection.Database.ScriptEvaluate(luaScript.ExecutableScript, new[] { redisKey }, values, flags)); } } catch (RedisServerException ex) when(ex.Message.StartsWith("NOSCRIPT", StringComparison.OrdinalIgnoreCase)) { Logger.LogInfo("Received NOSCRIPT from server. Reloading scripts..."); LoadScripts(); // retry throw; } }
public RedisCacheClient(ConnectionMultiplexer connectionMultiplexer, ISerializer serializer = null, ILoggerFactory loggerFactory = null) { _logger = loggerFactory?.CreateLogger <RedisCacheClient>() ?? NullLogger.Instance; _connectionMultiplexer = connectionMultiplexer; _serializer = serializer ?? new JsonNetSerializer(); var setIfLower = LuaScript.Prepare(SET_IF_HIGHER); var setIfHigher = LuaScript.Prepare(SET_IF_LOWER); var incrByAndExpire = LuaScript.Prepare(INCRBY_AND_EXPIRE); var delByWildcard = LuaScript.Prepare(DEL_BY_WILDCARD); foreach (var endpoint in _connectionMultiplexer.GetEndPoints()) { _setIfHigherScript = setIfLower.Load(_connectionMultiplexer.GetServer(endpoint)); _setIfLowerScript = setIfHigher.Load(_connectionMultiplexer.GetServer(endpoint)); _incrByAndExpireScript = incrByAndExpire.Load(_connectionMultiplexer.GetServer(endpoint)); _delByWildcardScript = delByWildcard.Load(_connectionMultiplexer.GetServer(endpoint)); } }
public RedisClient(IDatabase client, LoadedLuaScript zpopMinScript) { _internalClient = client; _zpopMinScript = zpopMinScript; }
public Task <RedisResult> ScriptEvaluateAsync(LoadedLuaScript script, object parameters = null, CommandFlags flags = CommandFlags.None) { // TODO: The return value could contain prefixed keys. It might make sense to 'unprefix' those? return(script.EvaluateAsync(Inner, parameters, Prefix, flags)); }
internal Producer(RingBuffer queue) { Queue = queue; _script = LuaScript.Prepare(ScriptPreprocessor(File.ReadAllText("Scripts/publish.lua"))).Load(Queue.Server); }
public async Task <RedisResult> RunLuaScript(LoadedLuaScript script, RedisKey[] keys, RedisValue[] values) { return(await Redis.GetDatabase().ScriptEvaluateAsync(script.Hash, keys, values)); }
RedisResult IDatabase.ScriptEvaluate(LoadedLuaScript script, Object parameters, CommandFlags flags) => Multiplexer.Wait(((IDatabaseAsync)this).ScriptEvaluateAsync(script, parameters, flags));
public CacheService() { this.cache = Connection.GetDatabase(); ruleLuaScript = LoadLuaScriptForGetRule(@"Lua/getMultiHashWithParameter.lua"); }
public Task <RedisResult> ScriptEvaluateAsync(LoadedLuaScript script, object parameters = null, CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); }
public Task <RedisResult> ScriptEvaluateAsync(LoadedLuaScript script, object parameters = null, CommandFlags flags = CommandFlags.None) { return(_database.ScriptEvaluateAsync(script, parameters, flags)); }