add state.cache and cache some stuff
This commit is contained in:
parent
5b33721c87
commit
56be46d82e
4 changed files with 56 additions and 24 deletions
20
oml/cache.py
Normal file
20
oml/cache.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import time
|
||||||
|
|
||||||
|
class Cache(dict):
|
||||||
|
|
||||||
|
def __init__(self, ttl=10):
|
||||||
|
self._ttl = ttl
|
||||||
|
self._added = {}
|
||||||
|
|
||||||
|
def get(self, key):
|
||||||
|
if key in self._added:
|
||||||
|
if self._added[key] < time.time():
|
||||||
|
del self._added[key]
|
||||||
|
del self[key]
|
||||||
|
return
|
||||||
|
return dict.__getitem__(self, key)
|
||||||
|
|
||||||
|
def set(self, key, value, ttl=None):
|
||||||
|
ttl = ttl or self._ttl
|
||||||
|
self._added[key] = time.time() + ttl
|
||||||
|
dict.__setitem__(self, key, value)
|
|
@ -4,6 +4,7 @@ from __future__ import division
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
import hashlib
|
||||||
|
|
||||||
from oxtornado import actions
|
from oxtornado import actions
|
||||||
|
|
||||||
|
@ -44,24 +45,28 @@ def find(data):
|
||||||
if 'group' in q:
|
if 'group' in q:
|
||||||
names = {}
|
names = {}
|
||||||
groups = {}
|
groups = {}
|
||||||
items = [i.id for i in q['qs'].options(load_only('id'))]
|
key = 'group:' + hashlib.sha1(json.dumps(data)).hexdigest()
|
||||||
qs = models.Find.query.filter_by(key=q['group'])
|
g = state.cache.get(key)
|
||||||
if items:
|
if g is None:
|
||||||
qs = qs.filter(models.Find.item_id.in_(items))
|
items = [i.id for i in q['qs'].options(load_only('id'))]
|
||||||
for f in qs.values('value', 'findvalue'):
|
qs = models.Find.query.filter_by(key=q['group'])
|
||||||
value = f[0]
|
if items:
|
||||||
findvalue = f[1]
|
qs = qs.filter(models.Find.item_id.in_(items))
|
||||||
if findvalue not in groups:
|
for f in qs.values('value', 'findvalue'):
|
||||||
groups[findvalue] = 0
|
value = f[0]
|
||||||
groups[findvalue] += 1
|
findvalue = f[1]
|
||||||
names[findvalue] = value
|
if findvalue not in groups:
|
||||||
g = [{'name': names[k], 'items': groups[k]} for k in groups]
|
groups[findvalue] = 0
|
||||||
else:
|
groups[findvalue] += 1
|
||||||
g = []
|
names[findvalue] = value
|
||||||
if 'sort' in q:
|
g = [{'name': names[k], 'items': groups[k]} for k in groups]
|
||||||
g.sort(key=lambda k: k[q['sort'][0]['key']])
|
else:
|
||||||
if q['sort'][0]['operator'] == '-':
|
g = []
|
||||||
g.reverse()
|
if 'sort' in q:
|
||||||
|
g.sort(key=lambda k: k[q['sort'][0]['key']])
|
||||||
|
if q['sort'][0]['operator'] == '-':
|
||||||
|
g.reverse()
|
||||||
|
state.cache.set(key, g)
|
||||||
if 'positions' in data:
|
if 'positions' in data:
|
||||||
response['positions'] = {}
|
response['positions'] = {}
|
||||||
ids = [k['name'] for k in g]
|
ids = [k['name'] for k in g]
|
||||||
|
|
|
@ -15,6 +15,7 @@ import websocket
|
||||||
import state
|
import state
|
||||||
import node.server
|
import node.server
|
||||||
import oxtornado
|
import oxtornado
|
||||||
|
from cache import Cache
|
||||||
|
|
||||||
from item.icons import IconHandler
|
from item.icons import IconHandler
|
||||||
from item.handlers import EpubHandler, ReaderHandler, FileHandler
|
from item.handlers import EpubHandler, ReaderHandler, FileHandler
|
||||||
|
@ -59,6 +60,7 @@ def run():
|
||||||
pid.write('%s' % os.getpid())
|
pid.write('%s' % os.getpid())
|
||||||
|
|
||||||
state.main = IOLoop.instance()
|
state.main = IOLoop.instance()
|
||||||
|
state.cache = Cache(ttl=10)
|
||||||
|
|
||||||
def start_node():
|
def start_node():
|
||||||
import user
|
import user
|
||||||
|
|
|
@ -276,12 +276,17 @@ class List(db.Model):
|
||||||
return self.public_id.encode('utf-8')
|
return self.public_id.encode('utf-8')
|
||||||
|
|
||||||
def items_count(self):
|
def items_count(self):
|
||||||
from item.models import Item
|
key = 'list:' + self.public_id
|
||||||
if self._query:
|
value = state.cache.get(key)
|
||||||
data = self._query
|
if key is None:
|
||||||
return Parser(Item).find({'query': data}).count()
|
from item.models import Item
|
||||||
else:
|
if self._query:
|
||||||
return len(self.items)
|
data = self._query
|
||||||
|
value = Parser(Item).find({'query': data}).count()
|
||||||
|
else:
|
||||||
|
value = len(self.items)
|
||||||
|
state.cache.set(key, value)
|
||||||
|
return value
|
||||||
|
|
||||||
def json(self):
|
def json(self):
|
||||||
r = {
|
r = {
|
||||||
|
|
Loading…
Reference in a new issue