I have an application on Java Spring that uses Redis fore some caching. Is there a way how to get a key or key-value pair that was added to the Redis last?
I have also 3 different types of values (Entities) that are stored to the Redis. Is there a way how get the latest record of one exact type of "value"?
Is Redis even suitable for such kind of things?
No Redis doesn't have this built-in functionality. You need to do it manually.
Whenever you set a key, you need to set that key name to an another key such as latest:key
set entity:1 value:1
set latest:key entity:1
get latest:key
You may also use hash to set the latest's key as field and value as hash value.
hset latest:key entity:1 value:1
hgetall latest:key
Related
In redis I'm planning to store key as a unique string and value will be a list.
I have a use case where I need to do 2 things.
First, I need to get all the values associated with a key by providing the key as input.
Second, I want to get all the keys associated with a value by providing one of the value in the values list.
Second part is where I need the advice, how we can achive this ?
I cannot get all the keys or key value pair and loop through because I will have millions of entries in Redis.
As mentioned in the comment above the retrieving of all keys with associated value at will probably sometimes create a performance issue as this will be a run through large entries.As also suggested in the official documentation about retrieving data from the memory caches you can try and use the following Redis command to get the value and see if that is what can solve your purpose.
GET
MGET
I currently have a scenario where we are using REDIS to store string field-value pairs within a hashed set HSET.
The original reasoning behind using hashed sets instead of just sets is ease of retrieving the records using HSCAN inside a GUI Search Bar as opposed to just SCAN because it's easier to get the length of a hash to use in the COUNT field.
I read in the Redis documentation that both GET and HGET commands execute with O(1) time complexity, but a member of my team thinks that if I store all the values inside a single key then it basically returns the entire hash during HGET instead of the singular field-value that I need.
So for a made up but similar example:
I have a Redis instance with a single Hashed Set called users.
The hashed set has 150,000 field:value pairs of username:email
If when I execute hget users coolguy, is the entire hash getting returned or just the email for user coolguy?
First of all, HSET is not a hash set, it creates a hash table. The mechanism behind the hash table and set (which is indeed a hash set) in redis is the same, the difference is mainly that the hash table has values.
To answer your question:
If when I execute hget users coolguy, is the entire hash getting returned or just the email for user coolguy?
Just the email for that user. You can also use HMGET to get the emails of multiple users at once. It's O(1) for each user you fetch, or O(n) for n users.
How can i sort my redis cache.
The data:
SADD key '{"id":250,"store_id":3,"url_path":"\/blog\/testblog123123",
"status":"Published","title":"TestBlog123123",
'"description":"","image":null,"description_2":"",
"date":"2017-04-17","blogcategory":"Category 3"}'
Next I need to sort my KEY by id.
This works:
SORT key BY *->id DESC
... but only when:
id > 10
because redis sort ONLY first number.
Maybe I should use another command to add, but I need JSON format.
You could use a sorted set from scratch?
ZADD key 250 '{"id":250,"store_id":3,"url_path":"\/blog\/testblog123123",
"status":"Published","title":"TestBlog123123",
'"description":"","image":null,"description_2":"",
"date":"2017-04-17","blogcategory":"Category 3"}'
I am also not sure why to use Set here at all, because uniqueness of a set element will only be guaranteed for the whole JSON string. And if your JSON serializer changes order of two fields in JSON dict, it will produce another string which is unique again and you'll end up with a dangling old string. Same applies, if you add more fields to the string.
I have N services that use M redis as the remote distributed cache. Suppose now multiple services want to retrieve the same key, and the following pseudo codes are how the work is done:
redisClient = getRedisClientByConsistentHash(key)
value = redisClient.get(key)
if value not exist
value = getValueFromSomewhereElse(key) // line4
redisClient set key value ex 1 nx // line5
return value
So the problem is:
In "line4", if 2 applications retrieve different values, one is newer and the other is old(should be deprecated), it's possible that the call to store the old value will happen before the call to store the new value, thus the new value won't be stored in redis. If we introduce some distributed lock mechanism, the problem still remains.
If the Key Storage internally makes use of timestamp of key in a way such that if KeyA is required to be updated from ValueA to ValueB then this updation is possible only if ValueB is inserted at a time which is greater than last updated timestamp of KeyA. Then its guaranteed that only new values will be inserted in a particular Key Storage. OldValues cannot overwrite NewValues (Timestamp Based Protocol). (Don't know whether redis follows Timestamp Based Protocol).
Both of your 2 applications (say, A, B) tried to fetch key from their respective primary redisClient and did not find the key and hence they went to fetch the key from SomewhereElse and found the key, but A has old and B has new. In such case there are few questions:
1. What if A's or B's primary `redisClient` itself gave you a value which is old?
2. How you come to know the value which is fetched is old or new?
Solutions:
1. Use a value which has majority (i.e the value received from atleast [ceil(M+1)/2] redisClients). Ofcourse this involves querying atleast [ceil(M+1)/2] rediClients which seems expensive. (Paxos Theorem)
2. Depending upon application logic, most of the time you don't require latest values. That is, if the application requirement is to just check the presence of a value then it does not matter whether the value is old or new.
I'm not expert in redis so does anyone knows how can I create a key that can have subkeys, and these subkeys must have an expire time each one.
Is this possible in Redis??
It would be something like this:
[:keyX]
|
V
[:keyZ][:value]
|
V
EXPIRE keyZ 100
PS. the app is in ruby.
Thanks!
Redis does not have nested keys, although the Hash data type could work for you. Also, Redis expiry is only for keys - Hash fields, List elements or Sorted and regular Sets members can not be assigned with an independent TTL.
Your question does not detail why you're looking to do that (i.e. store keys under a "root" key and have each key expire on its own). You can get the per-key expiration effect by using plain ol' regular keys, or use a Hash to aggregate all the fields under one common key - but not both at the same time.
That said, if you really need this sort of functionality you can always try implementing it yourself - see here for a possible direction: Redis: To set timeout for a key value pair in Set