当前位置: 首页>>代码示例>>Python>>正文


Python cache.SimpleCache类代码示例

本文整理汇总了Python中werkzeug.contrib.cache.SimpleCache的典型用法代码示例。如果您正苦于以下问题:Python SimpleCache类的具体用法?Python SimpleCache怎么用?Python SimpleCache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了SimpleCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Metrics

class Metrics(object):

    def __init__(self):
        self._cache = SimpleCache()
        self._metrics = []

    def build_keyspace(self, fields):
        """
        Given: ["one", "two", "three"]
        Yield: "one", "one.two", "one.two.three"
        """
        current = set()
        for field in fields:
            current.add(field)
            yield ".".join(current)

    def index(self):
        if not self._metrics:
            r = requests.get("%s/metrics/index.json" % graphite_url)
            self._metrics = json.loads(r.text)

    def search(self, term):
        cache_key = term
        rv = self._cache.get(cache_key)

        if not rv:
            rv = [metric for metric in self._metrics if term in metric]

        self._cache.set(cache_key, rv, timeout=86400)
        return rv
开发者ID:ohlol,项目名称:socrates,代码行数:30,代码来源:metrics.py

示例2: HybridCache

class HybridCache(object):
    """
    A hybrid cache class that provide in-process cache that is backup up by out-of-process cache
    When setting a key it will set it in both in-process and out-of-process
    When getting a key it will try to retrieve first from in-process and if not, from out-of-process
    """

    def __init__(self, remote_addresses):
        self.in_proc_cache = SimpleCache(1000, 3600)    #todo - pass these are arguments
        self.remote_addresses = remote_addresses
        self.out_proc_cache = MemcachedCache(remote_addresses)

    def get(self, key):
        """
        get an item from the hybrid cache
        first in the in-process and then in the out-of-process
        in case the key does not exist in both return None
        if the value exist in out of process but not in-process then it is added
        """
        val = self.in_proc_cache.get(key)

        if val is None:
            val = MemcachedCache(self.remote_addresses).get(key)
            if val is not None:
                self.in_proc_cache.add(key, val, None)  #todo: for how long to cache?

        return val

    def add(self, key, value, timeout = 300):
        """
        store a key-value in the hybrid cache - both in and out-off process
        """
        #self.out_proc_cache.add(key, value, timeout = timeout)
        MemcachedCache(self.remote_addresses).add(key, value, timeout)
        self.in_proc_cache.add(key, value, timeout = timeout)
开发者ID:yoavfrancis,项目名称:CloudCachingProxy,代码行数:35,代码来源:hybrid_cache.py

示例3: _UserSessions

class _UserSessions(object):
  def __init__(self):
    self.cache = SimpleCache()

  def create(self, user_id):
    user = User.find(User.id == user_id)
    if user is None:
      return None
    sess = os.urandom(24)
    self.cache.set(sess, user_id)
    session['key'] = sess
    return sess

  def get(self):
    if 'key' not in session:
      return None
    key = session['key']
    user_id = self.cache.get(key)
    user = User.find(User.id == user_id)
    session['user'] = user
    return user

  def delete(self):
    if 'key' in session:
      self.cache.delete(session['key'])
      session.pop('key', None)
      session.pop('user', None)
开发者ID:carylF,项目名称:PCB,代码行数:27,代码来源:sessions.py

示例4: PostalBot

class PostalBot(TelegramBot):
    STR_USERNAME = 'USERNAME'
    STR_MESSAGE_SENT = "Message sent"
    SPAM_TIMEOUT = 10*60

    def __init__(self, tg_api_key, tg_lounge_id):
        super(PostalBot, self).__init__(tg_api_key, tg_lounge_id)
        self.cache = SimpleCache()

    def handle_stream_publish(self, data):
        keys = data.keys()
        if 'watch_url' in keys and 'username' in keys:
            c_key = self.STR_USERNAME + ':' + data['username']
            if not self.cache.get(c_key):
                message = '{username} went live on Postal\n{url}'.format(
                    username=data['username'],
                    url=data['watch_url']
                )
                self.send_tg_message(self.tg_lounge_id, message)
                self.cache.set(c_key, True, timeout=self.SPAM_TIMEOUT)
                return self.STR_MESSAGE_SENT
        return ''

    def handle_postal_new_post(self, data):
        keys = data.keys()
        if 'username' in keys and 'title' in keys:
            message = "New post by {username}\n{title}".format(
                username=data['username'],
                title=data['title']
            )

            if 'image_url' in keys and 'image_size' in keys:
                message += "\n{url} {size}".format(
                    url=data['image_url'],
                    size=humanize.naturalsize(data['image_size'], gnu=True)
                )

            if 'file_url' in keys and 'file_size' in keys:
                message += "\n{url} {size}".format(
                    url=data['file_url'],
                    size=humanize.naturalsize(data['file_size'], gnu=True)
                )

            self.send_tg_message(self.tg_lounge_id, message)
            return self.STR_MESSAGE_SENT
        return ''

    def handle_tg_update(self, data):
        keys = data.keys()

        if 'message' in keys:
            self.handle_tg_message(data['message'])
        return ''

    def handle_tg_message(self, message):
        # print "%s %s: %s" % (message['from']['first_name'], message['from']['last_name'], message['text'])
        pass
开发者ID:latenssi,项目名称:postal-bot,代码行数:57,代码来源:PostalBot.py

示例5: test_simplecache_get_dict

def test_simplecache_get_dict():
    """SimpleCache.get_dict bug"""
    cache = SimpleCache()
    cache.set("a", "a")
    cache.set("b", "b")
    d = cache.get_dict("a", "b")
    assert "a" in d
    assert "a" == d["a"]
    assert "b" in d
    assert "b" == d["b"]
开发者ID:EnTeQuAk,项目名称:werkzeug,代码行数:10,代码来源:test_cache.py

示例6: UtilityTestCase

class UtilityTestCase(unittest.TestCase):

    def setUp(self):
        self.c = SimpleCache()

    def test_werkzeug_cache_get_or_add_missing_key(self):
        self.assertEquals('bar', werkzeug_cache_get_or_add(self.c, 'foo', 'bar', 10))

    def test_werkzeug_cache_get_or_add_existing_key(self):
        self.c.set('foo', 'bar')
        self.assertEquals('bar', werkzeug_cache_get_or_add(self.c, 'foo', 'qux', 10))
开发者ID:fangbei,项目名称:flask-webcache,代码行数:11,代码来源:test_storage.py

示例7: ZopeTemplateLoader

class ZopeTemplateLoader(jinja2.BaseLoader):

    def __init__(self, parent_loader, base_path,
                 cache_templates=True, template_list=[]):
        self.parent_loader = parent_loader
        self.cache = SimpleCache()
        self.cache_templates = cache_templates
        self.path = base_path
        self.template_list = template_list

    def get_source(self, environment, template):
        def passthrough(cachable=True):
            up = self.parent_loader
            source, path, uptodate = up.get_source(environment, template)
            if not cachable:
                uptodate = lambda: False
            return source, path, uptodate

        if not template in self.template_list:
            return passthrough()

        path = "%s%s" % (self.path, template)
        source = self.cache.get(path)
        if not source:
            try:
                response = requests.get(path)
            except requests.exceptions.ConnectionError:
                return passthrough(cachable=False)

            if response.status_code != 200:
                return passthrough(cachable=False)

            # escape jinja tags
            source = response.text
            source = source.strip()
            source = source.replace("{%", "{{ '{%' }}").replace("%}", "{{ '%}' }}")
            source = source.replace("{{", "{{ '{{' }}").replace("}}", "{{ '}}' }}")

            # template blocks
            source = source.replace("<!-- block_content -->",
                                    "{% block natura2000_content %}{% endblock %}")
            source = source.replace("<!-- block_head -->",
                                    "{% block head %}{% endblock %}")

            # fix breadcrumb link
            source = source.replace('"%s"' % self.path,
                                    '"%s"' % flask.url_for('naturasites.index'))

            if self.cache_templates:
                self.cache.set(path, source)

        return source, path, lambda: False
开发者ID:eaudeweb,项目名称:natura2000db,代码行数:52,代码来源:loader.py

示例8: __init__

 def __init__(self):
     self.sc = SimpleCache()
     self.databaseService = database_service.DatabaseService()
     self.devicesControl = devices_control.DevicesControl()
     self.save_current_office_state(room_state.RoomState(0, self.format_current_time(), [], 0, 0, 0))
     self.save_current_bedroom_state(room_state.RoomState(1, self.format_current_time(), [], 0, 0, 0))
     print self.sc.get("current_office_state")
开发者ID:rsdsilveira,项目名称:webpfc,代码行数:7,代码来源:house_state_manager.py

示例9: __init__

 def __init__(self, parent_loader, base_path,
              cache_templates=True, template_list=[]):
     self.parent_loader = parent_loader
     self.cache = SimpleCache()
     self.cache_templates = cache_templates
     self.path = base_path
     self.template_list = template_list
开发者ID:eaudeweb,项目名称:natura2000db,代码行数:7,代码来源:loader.py

示例10: Word

class Word(object):
    def __init__(self):
        self.cache = SimpleCache(threshold=1000, 
                                 default_timeout=60*60)

    def find_word(self, url):
        """ if any of words in unused, then select one.
        """
        def generator(url):
            for l in [self.cache.get, 
                      self.find_unused_word, 
                      self.find_used_word]:
                yield l(url)
        for selected_word in generator(url):
            if bool(selected_word):
                self.cache.set(url, selected_word)
                return selected_word 

    def find_url(self, word):
        if exists_used_word_in_db(word):
            return get_url_in_db(word)
        return None

    def find_unused_word(self, url):
        # find one from unused
        for word in split_words(url):
            if exists_unused_word_in_db(word):
                return select_unused_word_in_db(word, url)

        # one random
        last_word = choose_last_unused_word_in_db()
        return select_unused_word_in_db(last_word, url)

    def find_used_word(self, url):
        words = {}
        for word in split_words(url):
            if exists_used_word_in_db(word):
                words.setdefault(word, 
                         get_last_modified_in_db(word))

        oldest_word = ""
        if bool(words):
            oldest_word = min(words) 
        else:
            oldest_word = choose_last_modified_used_word_in_db()
        return select_used_word_in_db(oldest_word, url)
开发者ID:zedoul,项目名称:url-shorten,代码行数:46,代码来源:word.py

示例11: setUp

 def setUp(self):
     self.recached = False
     def dispatcher(salt):
         self.recached = True
     self.c = SimpleCache()
     cfg = Config(preemptive_recache_seconds=10, preemptive_recache_callback=dispatcher)
     self.s = Store(self.c, cfg)
     self.r = Retrieval(self.c, cfg)
开发者ID:fangbei,项目名称:flask-webcache,代码行数:8,代码来源:test_storage.py

示例12: test_simplecache_set_many

def test_simplecache_set_many():
    """Make sure set_many works with sequences as well as dicts"""
    cache = SimpleCache()
    cache.set_many({0: 0, 1: 1, 2: 4})
    assert cache.get(2) == 4
    cache.set_many((i, i*i) for i in xrange(3))
    assert cache.get(2) == 4
开发者ID:Fak3,项目名称:werkzeug,代码行数:7,代码来源:test_cache.py

示例13: ImageSimpleCache

class ImageSimpleCache(ImageCache):
    """Simple image cache."""

    def __init__(self):
        """Initialize the cache."""
        super(ImageSimpleCache, self).__init__()
        self.cache = SimpleCache()

    def get(self, key):
        """Return the key value.

        :param key: the object's key
        :return: the stored object
        :rtype: `BytesIO` object
        """
        return self.cache.get(key)

    def set(self, key, value, timeout=None):
        """Cache the object.

        :param key: the object's key
        :param value: the stored object
        :type value: `BytesIO` object
        :param timeout: the cache timeout in seconds
        """
        timeout = timeout if timeout else self.timeout
        self.cache.set(key, value, timeout)

    def delete(self, key):
        """Delete the specific key."""
        self.cache.delete(key)

    def flush(self):
        """Flush the cache."""
        self.cache.clear()
开发者ID:inveniosoftware,项目名称:flask-iiif,代码行数:35,代码来源:simple.py

示例14: BGAPI

class BGAPI(object):

    # Timeout (in minutes)
    cache_timeout = 1440

    def __init__(self):
        self.cache = SimpleCache()
        with open(app.app.config["BG_API_KEY"], "r") as f:
            self.auth_params = json.load(f)

    def get(self, url_path, params={}):
        """Build a simple cache of the requested data"""
        rv = self.cache.get(url_path)
        if rv is None:
            params.update(self.auth_params)
            url = "https://api.biblegateway.com/3/" + url_path

            response = requests.get(url, params=params)
            if response.status_code != 200:
                request = response.request
                raise RuntimeError("{} request {} returned {}".format(request.method, request.url, response.status_code))
            rv = response.json()
            self.cache.set(url_path, rv, timeout=self.cache_timeout*60)
        return rv

    def list_translations(self):
        return self.get('bible')['data']

    def get_translation(self, xlation):
        return self.get('bible/{}'.format(xlation))['data'][0]

    def get_book_info(self, xlation, book_osis):
        all_books = self.get_translation(xlation)['books']
        for book in all_books:
            if book['osis'] == book_osis:
                return book
        raise RuntimeError("Invalid book {} in translation {}".format(book_osis, xlation))

    def get_passage(self, xlation, passage_osis):
        verse_json = self.get("bible/{}/{}".format(passage_osis, xlation))['data'][0]
        passage_json = verse_json['passages'][0]
        return {'reference': passage_json['reference'],
                'content': passage_json['content']}
开发者ID:BoringCode,项目名称:scripture-engagement-platform,代码行数:43,代码来源:bg_api.py

示例15: RecacheTestCase

class RecacheTestCase(unittest.TestCase):

    def setUp(self):
        self.recached = False
        def dispatcher(salt):
            self.recached = True
        self.c = SimpleCache()
        cfg = Config(preemptive_recache_seconds=10, preemptive_recache_callback=dispatcher)
        self.s = Store(self.c, cfg)
        self.r = Retrieval(self.c, cfg)

    def test_preemptive_recaching_predicate(self):
        m = Metadata(HeaderSet(('foo', 'bar')), 'qux')
        def mkretr(**kwargs):
            return Retrieval(self.c, Config(**kwargs))
        with a.test_request_context('/'):
            self.assertFalse(mkretr(preemptive_recache_seconds=10).should_recache_preemptively(10, m))
            self.assertFalse(mkretr(preemptive_recache_callback=lambda x: 0).should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(11, m))
            self.assertTrue(self.r.should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(10, m))
            self.c.clear()
            self.assertTrue(self.r.should_recache_preemptively(10, m))

    def test_preemptive_recaching_cache_bypass(self):
        fresh = Response('foo')
        with a.test_request_context('/foo'):
            self.s.cache_response(fresh)
            metadata = self.r.fetch_metadata()
        with a.test_request_context('/foo'):
            cached = self.r.fetch_response()
            self.assertEquals(cached.headers[self.r.X_CACHE_HEADER], 'hit')
        with a.test_request_context('/foo', headers={RECACHE_HEADER: metadata.salt}):
            self.assertRaises(RecacheRequested, self.r.fetch_response)
        with a.test_request_context('/foo', headers={RECACHE_HEADER: 'incorrect-salt'}):
            try:
                self.r.fetch_response()
            except RecacheRequested:
                self.fail('unexpected RecacheRequested for incorrect salt')
开发者ID:fangbei,项目名称:flask-webcache,代码行数:39,代码来源:test_storage.py


注:本文中的werkzeug.contrib.cache.SimpleCache类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。