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

View file

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

View file

@ -34,6 +34,7 @@ else:
preferences = pdict(os.path.join(config_path, 'preferences.json'), config['user']['preferences']) preferences = pdict(os.path.join(config_path, 'preferences.json'), config['user']['preferences'])
ui = pdict(os.path.join(config_path, 'ui.json'), config['user']['ui']) 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 = pdict(os.path.join(config_path, 'server.json'))
server_defaults = { server_defaults = {

View file

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