How to get count of keys in store1 in redis db1 database?
enter image description here
commant "count keys store1" not working. please help
thanks in advance.
The command to calculate length is based on the type, if the type is hash then HLEN key will return the length of the hash. If the type is list then LLEN key will return the length and similarly STRLEN key will return the size/length of the string type.
To identify the type you can use TYPE key command and used the respective length command to get the size.
Related
I am currently looking into using redis to manage a black and whitelist for asterisks to manage spam calls. Since redis is a key value store it is great to check if a phone number is in the db. I can store some additional info in my value part of store and use the phone number as a key. 2 question I have is
A) a phone number could be multiple times in the key as it would be for different users. So since the key has to be unique in db I assume best way would to use a key like user:phone number. Is there a way I could get all records for a given user: in key or can I only get data if I have full key
B) when I store phone number as key, can I use something like user:+2135551212 or will the + in key cause problems ?
To answer your 1st question: No, you don't need full key to fetch all records for a given user. Redis provides wildcard matches which you can use in your case. As example:
127.0.0.1:6379> set john:1234567890 johnOne
OK
127.0.0.1:6379> set john:0987654321 johnTwo
OK
127.0.0.1:6379> keys john*
1) "john:1234567890"
2) "john:0987654321"
127.0.0.1:6379>
I have set two keys both with user 'john' followed by different phone numbers, so when I need all the keys which starts with john, we can use 'keys john*' to get all the keys which starts with 'john'.
To answer your second part, yes you can use '+' in the keys without any problem. Again, as an example:
127.0.0.1:6379> set user:+1234567890 helloagain
OK
I'm working with redis 2.8 with win10-64. As you can see in the screenshot I have a number of long keys. Rather than type or cut and paste them , is there a way to get the value by selecting their number in the list (you can see my 2 failed attempts) ?
Can you select the value of a REDIS key by the number of key in list?
NO, You can't.
Because Redis-cli does not support this feature. And even though if you know the key, you can't fetch the data without knowing it's data type.
Suppose you have a set/HashMap corresponding to a key. For that key command get <key> will not work for that typed data.
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);
Can anyone help me to find a way to fetch all values from a hash(name:VALUE) in redis which contains large number of key value pairs(having names:VALUE:10 etc)
You can use HGETALL to get all the key value pairs .But i am curious to know why you want this?
Hope it helps!
If you have a use case where you want to get the products of java with this price range you can do this via.
jedis.zadd("JAVA_Product",100,"PID1");
jedis.zadd("JAVA_Product",10,"PID2");
jedis.zadd("JAVA_Product",11,"PID4");
jedis.zadd("JAVA_Product",1200,"PID3");
Set<String> rangeofProducts = jedis.zrangeByScore("JAVA_Product", 10,100);
rangeofProducts.forEach(System.out::println);
It will print all the products with price between 10 and 100.
Hope it helps!.
I want to use Redis as a random seed cache. When I want the value for a key, if nothing's there yet, I'll produce a random string and store it for later reuse.
How do I perform an atomic GET EXISTING OR SET AND RETURN THIS VALUE?
You could use SETNX to try and set the value first. Then the GET would give you the existing value or the new one you tried to set.
SETNX key value
This may return 0 or 1 if you care to know if this is a new value
It seems there is no single command that can do this. Using MULTI and WATCH:
First:
GET key
If null, then:
WATCH key
MULTI
SET key value
EXEC
If [null] (indicating the transaction aborted), the key was created in the meantime and must exist by now:
GET key