C1 C2 keysToEvict #C1 keysToEvict --- threadLocal; C2 --- medis
'''
关闭C1, isolation level: Read Committed
开启C1, isolation level: Repeatable Read, 无幻读,但少数场景依然是Read Committed
eg:
A B C
start
start
... X.update(...)
commit start
X.get()
commit <---C2 cache is loaded
X.get() <--可以读到B的update
...
commit
如果cache切面应用在Service层,需要在Service层提供额外的细粒度的方法对DAO层update/delete包装。
'''
def get(k):
if k in C1:
return C1[k]
if k in keysToEvict:
return None
if k in C2:
C1[k] = C2[k]
return C2[k]
return None
def evict(k):
C1.evict(k)
keysToEvict.add(k)
register a <TransactionSynchronization> to flush keysToEvict if neccessary
def put(k, v):
C1[k] = v
if tx is readonly:
C2[k] = v