home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1424 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  2.2 KB  |  55 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. from calibre.utils.date import utcnow
  8. from calibre.utils.ordered_dict import OrderedDict
  9.  
  10. class Cache(object):
  11.     
  12.     def __init__(self):
  13.         self.reset_caches()
  14.  
  15.     
  16.     def reset_caches(self):
  17.         self._category_cache = OrderedDict()
  18.         self._search_cache = OrderedDict()
  19.  
  20.     
  21.     def search_cache(self, search):
  22.         old = self._search_cache.pop(search, None)
  23.         if old is None or old[0] <= self.db.last_modified():
  24.             matches = self.db.data.search_getting_ids(search, self.search_restriction)
  25.             if not matches:
  26.                 matches = []
  27.             
  28.             self._search_cache[search] = (utcnow(), frozenset(matches))
  29.             if len(self._search_cache) > 50:
  30.                 self._search_cache.popitem(last = False)
  31.             
  32.         else:
  33.             self._search_cache[search] = old
  34.         return self._search_cache[search][1]
  35.  
  36.     
  37.     def categories_cache(self, restrict_to = frozenset([])):
  38.         base_restriction = self.search_cache('')
  39.         if restrict_to:
  40.             restrict_to = frozenset(restrict_to).intersection(base_restriction)
  41.         else:
  42.             restrict_to = base_restriction
  43.         old = self._category_cache.pop(frozenset(restrict_to), None)
  44.         if old is None or old[0] <= self.db.last_modified():
  45.             categories = self.db.get_categories(ids = restrict_to)
  46.             self._category_cache[restrict_to] = (utcnow(), categories)
  47.             if len(self._category_cache) > 20:
  48.                 self._category_cache.popitem(last = False)
  49.             
  50.         else:
  51.             self._category_cache[frozenset(restrict_to)] = old
  52.         return self._category_cache[restrict_to][1]
  53.  
  54.  
  55.