Example #1
0
        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 });
                }
            }
        }
        public void SetQuerry(string _queryText)
        {
            queryText = _queryText;
            prepared  = LuaScript.Prepare(queryText);

            loaded = prepared.Load(REDISConnector.REDISConnector.GetRedis().GetServer(server));
        }
Example #3
0
        private LoadedLuaScript LoadLuaScriptForGetRule(string scriptUrl)
        {
            string    script    = File.ReadAllText(scriptUrl);
            LuaScript luaScript = LuaScript.Prepare(script);
            var       endpoints = Connection.GetEndPoints();
            var       endpoint  = endpoints[0];
            IServer   server    = Connection.GetServer(endpoint);

            return(luaScript.Load(server));
        }
Example #4
0
        /// <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);
        }
        public RedisDatabase(string name)
        {
            if (!EnvironmentSettings.Redis.TryGetDatabase(name, out EnvironmentSettings.RedisSettings.RedisDatabaseEntry database))
            {
                throw new Exception("Unknown redis database: " + name);
            }

            this.m_redis    = ConnectionMultiplexer.Connect(database.ConnectionString);
            this.m_database = this.m_redis.GetDatabase();

            EndPoint[] endPoints = this.m_redis.GetEndPoints();
            LuaScript  luaScript = LuaScript.Prepare(@"
                local value = redis.call('GET', KEYS[1])
                if value == ARGV[1] then
                    return redis.call('DEL', KEYS[1]);
                end
                return false;");

            for (int i = 0; i < endPoints.Length; i++)
            {
                byte[] hash = luaScript.Load(this.m_redis.GetServer(endPoints[i])).Hash;

                if (hash.Length != 20)
                {
                    throw new Exception("RedisDatabase.ctor: hash length != 20");
                }

                if (this.m_removeIfScript != null)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        if (this.m_removeIfScript[j] != hash[j])
                        {
                            throw new Exception("RedisDatabase.ctor: hash mismatch");
                        }
                    }
                }
                else
                {
                    this.m_removeIfScript = hash;
                }
            }
        }