def get_from_cache(self, query, idx=0, limit=0, skey=None, order='asc'): """ Retreieve results from cache, otherwise return null. """ id = 0 idx = int(idx) limit = long(limit) stop = idx + limit # get upper bound for range dbname = self.dbname cdb = self.couchdb(dbname) if not cdb: return key = genkey(query) skey = ["%s" % key, timestamp()] ekey = ["%s" % key, self.future] options = {'startkey': skey, 'endkey': ekey} results = cdb.loadView('dasviews', 'query', options) try: res = [row['value'] for row in results['rows']] for row in results['rows']: row['id'] = id if limit: if id >= idx and id <= stop: yield row else: yield row id += 1 except: traceback.print_exc() return if res: self.logger.info("DASCouchcache::get_from_cache for %s" % query)
def incache(self, query): """ Check if query exists in cache """ dbname = self.dbname cdb = self.couchdb(dbname) if not cdb: return key = genkey(query) #TODO:check how to query 1 result, I copied the way from get_from_cache skey = ["%s" % key, timestamp()] ekey = ["%s" % key, self.future] options = {'startkey': skey, 'endkey': ekey} # results = cdb.loadView('dasviews', 'incache', options) results = cdb.loadView('dasviews', 'query', options) try: res = len(results['rows']) except: traceback.print_exc() return if res: return True return False
def clean_cache(self): """ Clean expired docs in couch db. """ dbname = self.dbname cdb = self.couchdb(dbname) if not cdb: return skey = 0 ekey = timestamp() options = {'startkey': skey, 'endkey': ekey} results = cdb.loadView('dasadmin', 'cleaner', options) ndocs = 0 for doc in results['rows']: cdb.queueDelete(doc['value']) ndocs += 1 self.logger.info("DASCouchcache::clean_couch, will remove %s doc's" \ % ndocs ) if not ndocs: return cdb.commit() # bulk delete cdb.compact() # remove them permanently
print(viewmap['map']) sys.exit(0) if opts.delete: if opts.system: msg = "Delete '%s' docs in '%s' couch DB" % \ (opts.system, opts.delete) DAS.delete(opts.delete, opts.system) else: msg = "Delete '%s' couch DB" % opts.delete DAS.delete(opts.delete) print(msg, end=' ') sys.exit(0) t1 = 0 t2 = timestamp() if opts.fromtime: t1 = opts.fromtime if opts.totime: t2 = opts.totime options = {'group':'true'} view = 'timer' design = 'dasviews' if opts.query: key = genkey(opts.query) # skey = '["%s", %s ]' % (key, t1) # ekey = '["%s", %s ]' % (key, t2) skey = '["%s", %s ]' % (key, timestamp()) ekey = '["%s", %s ]' % (key, 9999999999) options = {'startkey': skey, 'endkey': ekey}
print(viewmap['map']) sys.exit(0) if opts.delete: if opts.system: msg = "Delete '%s' docs in '%s' couch DB" % \ (opts.system, opts.delete) DAS.delete(opts.delete, opts.system) else: msg = "Delete '%s' couch DB" % opts.delete DAS.delete(opts.delete) print(msg, end=' ') sys.exit(0) t1 = 0 t2 = timestamp() if opts.fromtime: t1 = opts.fromtime if opts.totime: t2 = opts.totime options = {'group': 'true'} view = 'timer' design = 'dasviews' if opts.query: key = genkey(opts.query) # skey = '["%s", %s ]' % (key, t1) # ekey = '["%s", %s ]' % (key, t2) skey = '["%s", %s ]' % (key, timestamp()) ekey = '["%s", %s ]' % (key, 9999999999) options = {'startkey': skey, 'endkey': ekey}