You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Server AWS Elasticachehttps://ap-southeast-2.console.aws.amazon.com/elasticache/home?region=ap-southeast-2#redis:
Client (cmd)C:\Program Files\Redis\redis-cli.exe
SELECT 1
KEYS *
SET mykey "Hello"
KEYS *
Client (desktop)
C# Client : StackExchange (~= redis-cli)
// your connection manager to redis. Heavy to instanciate (hint: use SingleInstance)ConnectionMultiplexerredis=ConnectionMultiplexer.Connect("localhost");IDatabasedb=redis.GetDatabase(1);db.StringSet("mykey","Hello");stringvalue=db.StringGet("mykey");Console.WriteLine(value);// writes: "Hello"
We want a cache which can achieve the following operations over a list of companies (Scanning the keys is not an option).
GetById(id)
GetAll()
GetbyAlias(alias)
Solution1 (using strings, sets)
// getbyid(id)
company:id1 -> { payload1 }
company:id2 -> { payload2 }
company:id3 -> { payload3 }
// get all (variant 1 - with payload)
allcompanies -> ( payload1, payload2, payload3 )
// get all (variant 2 - by id)
allcompanies -> ( company:id1, company:id2, company:id3 )
// get by alias (variant 1 - with payload)
company:alias[BHP] -> payload1
company:alias[IBM] -> payload2
company:alias[IBM] -> payload3
// get by alias (variant 2 - by id)
company:alias[BHP] -> company:id1
company:alias[IBM] -> company:id2
company:alias[IBM] -> company:id2
Solution 2 (using hashsets)
// get by id & get all
company:master -> { (id1 -> payload1),(id2 -> payload2),(id3 -> payload3) }
// get by alias
company:byalias -> { (BHP -> payload1),(IBM -> payload2),(IBM -> payload3) }
Redis support transactions, as per definition, a transaction guarantee that no other write operation will occurs during while the transaction is pending.
Simplest demonstration using the C# client
// start the transactionvartransaction=_database.CreateTransaction();// add operations to the transaction context// Resharper will complain as the operation bundle in the transaction are awaitable not being explicitly awaited // Using #pragma warning disable 4014 will shut it. transaction.StringSetAsync("myKey1","val1");transaction.StringSetAsync("myKey2","val2");// if I care about the return (anti-CQS)vartask=transaction.StringSetAsync("myKey2","val2");// transaction executionawaittransaction.ExecuteAsync();// now 'task' must be 'completed', and its result will be awailablevarresult=task.Result;// pseudo code.
Transactions are per Redis instance. For distributed transactions see distibuted locks.
// Using stackExchange.Redisusing(ConnectionMultiplexerconnection=ConnectionMultiplexer.Connect("localhost")){IDatabasedb=connection.GetDatabase();ISubscribersubscriber=connection.GetSubscriber();subscriber.Subscribe("__keyspace@0__:*",(channel,value)=>{if((string)channel=="__keyspace@0__:users"&&(string)value=="expriy"){// Do stuff if some item is added to a hypothetical "users" set in Redis}});}
Keys with a time to live associated are expired by Redis in two ways:
When the key is accessed by a command and is found to be expired.
Via a background system that looks for expired keys in background, incrementally, in order to be able to also collect keys that are never accessed.
The expired events are generated when a key is accessed and is found to be expired by one of the above systems, as a result there are no guarantees that the Redis server will be able to generate the expired event at the time the key time to live reaches the value of zero. If no command targets the key constantly, and there are many keys with a TTL associated, there can be a significant delay between the time the key time to live drops to zero, and the time the expired event is generated.
Enabling KeySpace notifications (expiry only)
CONFIG SET notify-keyspace-events xKE
CONFIG GET notify-keyspace-events
All events (makes the whole system really chatty, not recommended for production systems)
CONFIG SET notify-keyspace-events AKE
CONFIG GET notify-keyspace-events
Test it with redis-cli (open a listener to event on db 0)
With the StackExchange client (create and open a listener to events on db 0)
// Using stackExchange.Redisusing(ConnectionMultiplexerconnection=ConnectionMultiplexer.Connect("localhost")){IDatabasedb=connection.GetDatabase();ISubscribersubscriber=connection.GetSubscriber();subscriber.Subscribe("__keyspace@0__:*",(channel,value)=>{if((string)channel=="__keyspace@0__:users"&&(string)value=="expriy"){// Do stuff if some item is added to a hypothetical "users" set in Redis}});}