add state.cache and cache some stuff

This commit is contained in:
j 2014-05-26 13:41:59 +02:00
parent 5b33721c87
commit 56be46d82e
4 changed files with 56 additions and 24 deletions

20
oml/cache.py Normal file
View 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)

View file

@ -4,6 +4,7 @@ from __future__ import division
import os
import json
import hashlib
from oxtornado import actions
@ -44,6 +45,9 @@ def find(data):
if 'group' in q:
names = {}
groups = {}
key = 'group:' + hashlib.sha1(json.dumps(data)).hexdigest()
g = state.cache.get(key)
if g is None:
items = [i.id for i in q['qs'].options(load_only('id'))]
qs = models.Find.query.filter_by(key=q['group'])
if items:
@ -62,6 +66,7 @@ def find(data):
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:
response['positions'] = {}
ids = [k['name'] for k in g]

View file

@ -15,6 +15,7 @@ import websocket
import state
import node.server
import oxtornado
from cache import Cache
from item.icons import IconHandler
from item.handlers import EpubHandler, ReaderHandler, FileHandler
@ -59,6 +60,7 @@ def run():
pid.write('%s' % os.getpid())
state.main = IOLoop.instance()
state.cache = Cache(ttl=10)
def start_node():
import user

View file

@ -276,12 +276,17 @@ class List(db.Model):
return self.public_id.encode('utf-8')
def items_count(self):
key = 'list:' + self.public_id
value = state.cache.get(key)
if key is None:
from item.models import Item
if self._query:
data = self._query
return Parser(Item).find({'query': data}).count()
value = Parser(Item).find({'query': data}).count()
else:
return len(self.items)
value = len(self.items)
state.cache.set(key, value)
return value
def json(self):
r = {