I have a Redis sorted set where the score is a Unix timestamp and the member is a counter. Is there a way to increment this counter effectively? It seems Redis only provides functionality for incrementing the score.
Members of sets (sorted or not) are immutable - there is no way/reason to update them: Efficient way redis set member update
Related
Let's say that i have a hyperloglog in redis which counts messages is there any provisions whereby I can to some degree account for delete messages?
No, the HyperLogLog doesn't support the concept of deletion. Instead, use a different counter (could be an integer, Set or HyperLogLog) and subtract the totals.
I'm using Redis implementation of HyperLogLog to count distinct values for given keys.
The keys are based on hour window. After the calendar hour changes, I want to reset the count of incoming values. I don't see any direct API for 'clearing' up the values through Jedis.
SET cannot be used here because it would corrupt the hash. Is there a way to correctly "reset" the count for a given key?
Use the DEL command to delete the key, which will effectively reset the count.
I don't want to use keys * command because it is O(N).
Is it possible to keep the newest Objects in redis?
Not using KEYS is definitely the way to go. Use a Sorted Set to store key names when you create them and set the score to the time of creation. You can the fetch key names by their creation time with ZRANGEBYSCORE and don't forget trimming the older keys from it using ZREMRANGEBYSCORE.
I am using jedis, a redis java client. I have a queue of string items. As per normal I am using lpush lpop rpush rpop for the necessary operations. But I will like to set expiry for each individual items in the queue. Is it possible?
This is not possible in redis by design for the sake of keeping redis simple and fast.
You can either store an expire value along with the string in the list, or store a separate list of expire times to let your application know if the key has expired.
There is also an alternative solution discussed here. You can store values in a sorted set with expire timestamps as scores and only select those members, whose scores are greater than certain timestamp. (This of course leaves it up to your app to clear the expired elements in a set)
What would be a good way to create a fresh sequence of serial numbers on a per-day basis in a SQL database?
Something like the auto-increment feature of integer columns in some database systems.
I'm creating/recording transactions, and the transaction is identified by the pair 'date,serial no'.
That depends on what kind of database you have. If you have access to global variables or generators, just reset it each day and use it to seed your serial number column. If not, you can store the value in a table and look it up to seed your column, again resetting it each day.
Don't forget to increment the seeds manually if necessary. (Generators are a special kind of global variable that can auto-increment themselves if set up to do so. Other variables and certainly a record in a table do not.)
To reset the value, just set a trigger on insert that checks if COUNT(DATE = today) is 0. If so, reset the value.
How about a specific table just for this purpose?
create table AvailableSerialNumbers (
AvailableOn datetime primarykey,
NextAvailableNumber int
)
You'd have to populate this in advance, but that's pretty straight-forward (make sure this is done automatically and not manually).
Note that if you have a large number of serial number records being created at the same time the create logic will bottleneck on updating the AvailbleSerialNumbers record. The easiest solution to that problem is to define multiple AvailableSerialNumbers records per day (say 100 of them) and randomly choose 1 to update. If using this approach them instead of a "NextAvailableNumber" field it should be a from/to range. When the range hits 0 delete the range record.
The best way is to use the date as part of the number, such as having 080216001 through 080216999 for today. Can you have non-contiguous numbers?