-1

I am using Redis for cache in my application which is configured in spring beans, spring-data-redis 1.7.1, jedis 2.9.0. I would like to know how to set the race condition ttl in the configuration.

Please comment if you have any suggestions.

teja
  • 11
  • 1
  • 3
  • 2
    Explain what do you mean by race condition and race condition ttl. – Imaskar Jun 19 '18 at 13:49
  • Race condition ttl is to lock down the key for recalculation when key reaches expiry time . It prevents multiple processes from trying to simultaneously repopulate the same key . – teja Jun 20 '18 at 03:13
  • https://github.com/anujdas/atomic_redis_cache – teja Jun 20 '18 at 03:17

1 Answers1

0

If I understand you right, you want the same as that Ruby repo, but in Java.
For that case you may want to put a technical lock key along the one you need.

get yourkey
(nil)
get <yourkey>::lock
// if (nil) then calculate, if t then wait. assuming (nil) here
setex <yourkey>::lock 30 t
OK
// calcultions
set <yourkey> <result>
OK
del <yourkey>::lock
(integer) 1

Here with setex you set a lock key with TTL of 30 sec. You can put another TTL if you want.

There is one problem with the code above - some time will pass before checking a lock and aquiring it. To properly aquire a lock EVAL can be used: eval "local lk=KEYS[1]..'::lock' local lock=redis.call('get',lk) if (lock==false) then redis.call('setex',lk,KEYS[2],'t') return 1 else return 0 end" 2 <yourkey> 30 This either returns 0 if there is no lock or puts a lock and returns 1.

Imaskar
  • 2,773
  • 24
  • 35
  • Yeah, But its says default lock time is 30s . I want to override that lock time ..perhaps decrease it to 10s. – teja Jun 20 '18 at 05:45
  • put 10 as the second parameter of `setex` – Imaskar Jun 20 '18 at 05:51
  • how to set it in spring bean configuration ...?? I can set expiration and max redirect in spring beans ... in same way I am looking for race condition ttl as well – teja Jun 20 '18 at 05:59
  • I don't know if spring-data-redis have this functionality. You probbly have to do it manually. – Imaskar Jun 20 '18 at 06:18
  • I found an unanswered question (https://stackoverflow.com/questions/47052312/spring-boot-starter-data-redis-cacheablesync-true-sees-not-work) that hints that synchronous update doesn't work in spring-data-cache at all. But it might had changed since then. – Imaskar Jun 20 '18 at 07:50