I am storing data in redis using JCA(java caching api) where key is String and value is Object which is JSON string.
I have a requirement to perform partial update to cache value instead of retrieving cache value using key and then modify attribute and perform put operation with latest cache value
{
"attribute1" : "value1",
"attribute2 " : [
{
"attribute3" : "value3"
}
]
}
Above is sample json format. As explained above is it possible to update value of attribute1
from value1
to value2
without geting cache value using key in redis
2
Answers
You can use a Lua script, so that using the CJSON Lua library you update the item. I have shared a similar example on How to nest a list into a structure in Redis to reduce top level?
Not familiar with JCA, so not sure if your client would make it simple to send an EVAL command.
Assuming you are using JCache API (ie JSR-107), you can use
Cache#invoke(K key, EntryProcessor<K,V,T> entryProcessor, Object... arguments)
to perform an update in-place instead of get-then-put. According toEntryProcessor
javadoc,Cache#invoke
is executed atomically on the key, so you don’t have to worry about concurrent modifications to the same cache entry.