I want to store a set of strings against a key in a Redis in a single step. Presently, I am using multi-exec mechanism for storing by iterating over the set.
I am using spring-data-redis library for Redis operation.
Is it possible to store a set of strings in a Redis in a single step ?
Yes, the RPUSH/LPUSH commands accept multiple values, beginning with Redis 2.4. Example:
LPUSH key value1 value2 value3
If you're using a set rather than a list, it's the SADD command:
SADD key value1 value2 value3
In Spring Data Redis, StringRedisConnection supports multiple values in the rPush, lPush, and sAdd methods.
Do you want to store a list or a set? (You refer to both.)
In general you can store your list of strings as a single JSON string.
With Spring data API for Redis , you can set a list or object using
eg:-
opsForList.leftPush("key", object);
Related
I want to use Hash of Redis and store a JSON Object in one key and Array in other.
How i can achieve this.
For example when i run this:
redis.hmset(`channel`, 'key1', 'someValue', 'key2', [{x: 1}])
then it gives the error.
But if i change the value of key2 to string it store it in form of string.
I wnat to store the JSON data. How can i do so?
I am using Node and redis npm package.
There is no native way to store an array in Redis Hash.
But, you can use the Redis Module RedisJSON which adds native JSON support to Redis. It has couple of node.js clients you can use.
e.g. redis-modules-sdk and redis-rejson
I have a problem with getting data from Redis. I want to get the object using not Id column. Can I do this in Redis? Or another case when I want to get the object using Id column, but I want to have my name of this column, not just 'Id', but I have the problem with this too.
Redis is primarily a Name/Value store.
You can set the NAME to anything you want. If you want to call it 'Id' you can. If you want to call it 'x-y-z' you can too and store the VALUE along with it.
Redis also supports Hashes. If you want to get an object "using not Id column" you can store similar object types in a Hash and retrieve the entire HashSet then find the object inside the list on the client side. This is not efficient if storing large objects or if you have many objects in a list.
Ex:
HMSET user:1000 username antirez password P1pp0 age 34
HGETALL user:1000
HSET user:1000 password 12345
HGETALL user:1000
Here is a list of datatypes Redis Supports.
https://redis.io/topics/data-types
The 2 popular libraries for using C# and Redis are:
https://github.com/ServiceStack/ServiceStack.Redis
https://github.com/StackExchange/StackExchange.Redis
I have the n number of keys in my redis server with some data. Now i want to check what all keys got created in last two months. How to check this. Is there any way to sort all cache keys in redis-cli by creation time or anything?
Redis doesn't store this information. You need to do this explicitly. There are many ways you can achieve this. Some of them are:
SET time or date or datetime string when setting key
ex:
SET key1 data
SET key1:date "12-JULY-2018"
Make data Object type - Add an explicit key of created at and then store it to Redis. Then sort it in your own application.
Create Sets/Lists of each hour/day/month and keep pushing all the keys to those lists. You can then retrieve keys for each hour/day/month. Now get data using these keys.
ex:
SET key1 data // At this point date is "12-JULY-2018"
SADD "JULY-SET" key1
Now you can get all keys of JULY by doing this:
SMEMBERS "JULY-SET"
Given I have a list stored in a key "A", how can I duplicate that list in a different key "B"?
I know that for non list values, I can "get", and then "set". But for lists when I try to get it, I see the WRONGTYPE operation error.
Redis enables 5 different data structures, such as:
Key values
Strings
Hashs
Lists
Sets
Each of the data structure has its own commands.
In order to get the current list you should use the LRANGE command.
The prefix L referes to the List data structure.
(Redis Set data structure has a related command to range by using SETRANGE)
If you read the Redis LRANGE documentation you will understand how to use it.
Here is the brief code you may use:
LRANGE mylist 0 -1
Where mylist is the list you get the values from.
The offsets start and stop are zero-based indexes, with 0 being the
first element of the list (the head of the list), 1 being the next
element and so on.
-1 is used to describe the last element in the list.
Now you should use the LPUSH or RPUSH depends on the list side you wish to insert the old elements into the new list.
You should use LRANGE to get all elements of the first list, then use LPUSH or RPUSH to put these elements into the second list.
Another way to duplicate a key's value, regardless the data structure, is to DUMP it and then RESTORE it into the new key. In most cases this approach is also the quickest.
For example i have an array/json with 100000 entries cached with Redis / Predis. Is it posible to update or delete 1 or more entries or do i have to generate the whole array/json of 100000 entries? And how can I achieve that?
It is about how you store them if you are storing it as a string then no,
set key value
get key -> will return you value
Here value is your json/array with 10000 entries.
Instead if you are storing it in a hash . http://redis.io/commands#hash
hmset key member1 value1 member2 value2 ...
then you can update/delete member1 separately.
If you are using sets/lists you can achieve it with similar commands like lpush/lpop, srem etc.
Do read the commands section to know more about redis data structures which will give you more flexibility in selecting your structure.
Hope this helps
If you are using cache service, you have to:
get data from cache
update some entries
save data back in cache
You could use advanced Redis data structures like Hashes, but you it is not supported by Cache service, you would need to write you own functions.
Thanks Karthikeyan Gopall, i made an example:
Here i changed field1 value and it works :)
$client = Redis::connection();
$client->hmset('my:hash', ['field1'=>'value1', 'field2'=>'value2']);
$changevalue= Redis::hset('my:hash' , 'field1' , 'newvaluesssssssssss');
$values1 = Redis::hmget('my:hash' , 'field1');
$values2 = Redis::hmget('my:hash' , 'field2');
print_r($values1);
print_r($values2);