base.py :  » Blog » PyBlosxom » pyblosxom-1.5-rc1 » Pyblosxom » cache » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Blog » PyBlosxom 
PyBlosxom » pyblosxom 1.5 rc1 » Pyblosxom » cache » base.py
#######################################################################
# This file is part of PyBlosxom.
#
# Copyright (c) 2003-2006 Wari Wahab
# Copyright (c) 2003-2010 Will Kahn-Greene
#
# PyBlosxom is distributed under the MIT license.  See the file
# LICENSE for distribution details.
#######################################################################
"""
The cache base class.  Subclasses of this class provide caching for
blog entry data in PyBlosxom.
"""

class BlosxomCacheBase:
    """
    Base Class for Caching stories in pyblosxom.

    A cache is a disposable piece of data that gets updated when an entry
    is in a fresh state.

    Drivers are to subclass this object, overriding methods defined in
    this class.  If there is an error in creating cache data, be as quite
    as possible, document how a user could check whether his cache works.

    Driver should expect empty caches and should attempt to create them from
    scratch.

    @ivar _config: String containing config on where to store the cache.
        The value of config is derived from C{py['cacheConfig']} in config.py.
    @type _config: string
    """
    def __init__(self, req, config):
        """
        Constructor - setup and load up the cache

        @param req: the request object
        @type req: Request

        @param config: String containing config on where to store the cache
        @type config: string
        """
        self._request = req
        self._config = config

        self._entryid = ""
        self._entrydata = {}

    def load(self, entryid):
        """
        Try to load up the cache with entryid (a unique key for the entry)

        @param entryid: The key identifier for your cache
        @type entryid: string
        """
        self._entryid = entryid # The filename of the entry
        self._entrydata = {}    # The data of the entry

    def getEntry(self):
        """
        Gets the data from the cache, returns a dict or an empty dict.
        """
        return self._entrydata

    def isCached(self):
        """
        Returns 0 or 1 based on whether there is cached data, returns 0 is
        cache data is stale

        @returns: 0 or 1 based on cache
        @rtype: boolean
        """
        return 0

    def saveEntry(self, entrydata):
        """
        Store entrydata in cache

        @param entrydata: The payload, usually a dict
        @type entrydata: dict
        """
        pass

    def rmEntry(self):
        """
        Remove cache entry: This is not used by pyblosxom, but used by
        utilities.
        """
        pass

    def close(self):
        """
        Override this to close your cache if necessary.
        """
        pass

    def __getitem__(self, key):
        """
        Convenience function to make this class look like a dict.
        """
        self.load(key)
        if not self.has_key(key):
            raise KeyError
        return self.getEntry()

    def __setitem__(self, key, value):
        """
        Synonymous to L{saveEntry}
        """
        self.load(key)
        self.saveEntry(value)

    def __delitem__(self, key):
        """
        Convenience function to make this look more like a dict.
        """
        self.load(key)
        self.rmEntry()

    def has_key(self, key):
        """
        Convenience function to make this look more like a dict.
        """
        self.load(key)
        return self.isCached()

    def keys(self):
        """
        List out a list of keys for the cache, to be overridden by a subclass
        if a full dict interface is required.
        """
        return []

    def get(self, key, default=None):
        """
        Convenience function to make this look more like a dict.
        """
        try:
            return self.__getitem__(key)
        except KeyError:
            return default


class BlosxomCache(BlosxomCacheBase):
    """
    A null cache.
    """
    pass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.