cache list count in pdict and clear where needed

This commit is contained in:
j 2016-01-05 20:44:41 +05:30
parent e5d4a75358
commit 592f79dc39
4 changed files with 34 additions and 5 deletions

View file

@ -132,13 +132,13 @@ class Changelog(db.Model):
if user not in i.users:
i.users.append(user)
i.update()
return True
else:
i = Item.get_or_create(itemid, info)
i.modified = ts2datetime(timestamp)
if user not in i.users:
i.users.append(user)
i.update()
user.clear_smart_list_cache()
return True
def action_edititem(self, user, timestamp, itemid, meta):
@ -166,6 +166,7 @@ class Changelog(db.Model):
i.update_meta(meta)
i.modified = ts2datetime(timestamp)
i.save()
user.clear_smart_list_cache()
return True
def action_removeitem(self, user, timestamp, itemid):
@ -178,6 +179,8 @@ class Changelog(db.Model):
i.update()
else:
i.delete()
user.clear_list_cache()
user.clear_smart_list_cache()
return True
def action_addlist(self, user, timestamp, name, query=None):
@ -191,6 +194,7 @@ class Changelog(db.Model):
if 'name' in new:
l.name = new['name']
l.save()
user.clear_list_cache()
return True
def action_orderlists(self, user, timestamp, lists):
@ -208,12 +212,15 @@ class Changelog(db.Model):
l = List.get(user.id, name)
if l:
l.remove()
user.clear_list_cache()
return True
def action_addlistitems(self, user, timestamp, name, ids):
from user.models import List
l = List.get_or_create(user.id, name)
l.add_items(ids)
user.clear_list_cache()
user.clear_smart_list_cache()
return True
def action_removelistitems(self, user, timestamp, name, ids):
@ -221,6 +228,8 @@ class Changelog(db.Model):
l = List.get(user.id, name)
if l:
l.remove_items(ids)
user.clear_list_cache()
user.clear_smart_list_cache()
return True
def action_editusername(self, user, timestamp, username):
@ -267,6 +276,7 @@ class Changelog(db.Model):
m = Metadata.get_or_create(key, value)
if m.edit(data):
m.update_items()
user.clear_smart_list_cache()
return True
def action_resetmeta(self, user, timestamp, key, value):
@ -274,4 +284,5 @@ class Changelog(db.Model):
m = Metadata.get(key, value)
if m and m.timestamp < timestamp:
m.reset()
user.clear_smart_list_cache()
return True

View file

@ -153,6 +153,7 @@ def edit(data):
for key in list(state.cache):
if key.startswith('group:'):
state.cache.delete(key)
state.user().clear_smart_list_cache()
return response
actions.register(edit, cache=False)
@ -167,6 +168,7 @@ def remove(data):
if 'ids' in data and data['ids']:
for i in models.Item.query.filter(models.Item.id.in_(data['ids'])):
i.remove_file()
state.user().clear_smart_list_cache()
return {
'items': []
}

View file

@ -34,6 +34,7 @@ else:
preferences = pdict(os.path.join(config_path, 'preferences.json'), config['user']['preferences'])
ui = pdict(os.path.join(config_path, 'ui.json'), config['user']['ui'])
lists_cache = pdict(os.path.join(config_path, 'lists_cache.json'), {})
server = pdict(os.path.join(config_path, 'server.json'))
server_defaults = {

View file

@ -89,6 +89,17 @@ class User(db.Model):
'user': self.name
}] + [l.json() for l in self.lists.order_by('index_')]
def clear_list_cache(self):
for key in list(settings.list_cache):
if key.startswith(self.id + ':'):
del settings.list_cache[key]
def clear_smart_list_cache(self):
smart_lists = [':' + l.name for l in List.query.filter_by(type='smart')]
for key in list(settings.list_cache):
if key in smart_lists:
del settings.list_cache[key]
def update_peering(self, peered, username=None):
was_peering = self.peered
if peered:
@ -118,6 +129,9 @@ class User(db.Model):
if not i.users:
i.delete()
Changelog.query.filter_by(user_id=self.id).delete()
if self.id in settings.ui['showFolder']:
del settings.ui['showFolder'][self.id]
self.clear_list_cache()
self.save()
if was_peering:
Changelog.record(state.user(), 'removepeer', self.id)
@ -287,16 +301,17 @@ class List(db.Model):
return self.public_id
def items_count(self):
key = 'list:' + self.public_id
value = state.cache.get(key)
if value is None:
key = self.find_id
if key in settings.lists_cache:
value = settings.lists_cache[key]
else:
from item.models import Item
if self._query:
data = self._query
value = Parser(Item).find({'query': data}).count()
else:
value = len(self.items)
state.cache.set(key, value, ttl=90)
settings.lists_cache[key] = value
return value
def json(self):