Clear cache for all records of ModelSingleton
As write and delete can be called with any ids for records and the super calls are always on the real record, the cache is only cleared for the real record. So ModelSingleton must ensure to clear the cache for any record ids. issue7205 review44731002
This commit is contained in:
1 parent
1c32a02b64
commit
57248f9655
2 files changed
+31
-4
No files matched your search
@@ -1,6 +1,7 @@
|
||||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
# this repository contains the full copyright notices and license terms.
|
||||
from trytond.model import ModelStorage
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
|
||||
class ModelSingleton(ModelStorage):
|
||||
@@ -57,14 +58,26 @@ class ModelSingleton(ModelStorage):
|
||||
args = []
|
||||
for values in actions[1:None:2]:
|
||||
args.extend(([singleton], values))
|
||||
return super(ModelSingleton, cls).write(*args)
|
||||
super(ModelSingleton, cls).write(*args)
|
||||
# Clean local cache of original records
|
||||
for record in sum(actions[0:None:2], []):
|
||||
local_cache = record._local_cache.get(record.id)
|
||||
if local_cache:
|
||||
local_cache.clear()
|
||||
# Clean transaction cache of all ids
|
||||
for cache in Transaction().cache.itervalues():
|
||||
if cls.__name__ in cache:
|
||||
cache[cls.__name__].clear()
|
||||
|
||||
@classmethod
|
||||
def delete(cls, records):
|
||||
singleton = cls.get_singleton()
|
||||
if not singleton:
|
||||
return
|
||||
return super(ModelSingleton, cls).delete([singleton])
|
||||
if singleton:
|
||||
super(ModelSingleton, cls).delete([singleton])
|
||||
# Clean transaction cache of all ids
|
||||
for cache in Transaction().cache.itervalues():
|
||||
if cls.__name__ in cache:
|
||||
cache[cls.__name__].clear()
|
||||
|
||||
@classmethod
|
||||
def copy(cls, records, default=None):
|
||||
|
||||
@@ -129,6 +129,20 @@ class ModelSingletonTestCase(unittest.TestCase):
|
||||
singletons = Singleton.search([('name', '=', 'bar')])
|
||||
self.assertEqual(singletons, [])
|
||||
|
||||
@with_transaction()
|
||||
def test_all_cache_cleared(self):
|
||||
"Test all cache cleared"
|
||||
pool = Pool()
|
||||
Singleton = pool.get('test.singleton')
|
||||
|
||||
singleton, = Singleton.create([{'name': 'foo'}])
|
||||
singleton2 = Singleton(singleton.id + 1) # Use a different id
|
||||
singleton2.name # Fill the cache
|
||||
Singleton.write([singleton], {'name': 'bar'})
|
||||
singleton2 = Singleton(singleton2.id) # Clear local cache
|
||||
|
||||
self.assertEqual(singleton2.name, 'bar')
|
||||
|
||||
|
||||
def suite():
|
||||
return unittest.TestLoader().loadTestsFromTestCase(ModelSingletonTestCase)
|
||||
Reference in new issue
Block a user