local_session_caching.py :  » Database » SQLAlchemy » SQLAlchemy-0.6.0 » examples » beaker_caching » 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 » Database » SQLAlchemy 
SQLAlchemy » SQLAlchemy 0.6.0 » examples » beaker_caching » local_session_caching.py
"""local_session_caching.py

Create a new Beaker cache type + a local region that will store
cached data local to the current Session.

This is an advanced example which assumes familiarity
with the basic operation of CachingQuery.

"""

from beaker import cache,container
import collections

class ScopedSessionNamespace(container.MemoryNamespaceManager):
    """A Beaker cache type which will cache objects locally on 
    the current session.

    When used with the query_cache system, the effect is that the objects
    in the cache are the same as that within the session - the merge()
    is a formality that doesn't actually create a second instance.  
    This makes it safe to use for updates of data from an identity
    perspective (still not ideal for deletes though).

    When the session is removed, the cache is gone too, so the cache
    is automatically disposed upon session.remove().

    """
    
    def __init__(self, namespace, scoped_session, **kwargs):
        """__init__ is called by Beaker itself."""
        
        container.NamespaceManager.__init__(self, namespace)
        self.scoped_session = scoped_session
    
    @classmethod
    def create_session_container(cls, beaker_name, scoped_session):
        """Create a new session container for a given scoped_session."""
        
        def create_namespace(namespace, **kw):
            return cls(namespace, scoped_session, **kw)
        cache.clsmap[beaker_name] = create_namespace
        
    @property
    def dictionary(self):
        """Return the cache dictionary used by this MemoryNamespaceManager."""
        
        sess = self.scoped_session()
        try:
            nscache = sess._beaker_cache
        except AttributeError:
            sess._beaker_cache = nscache = collections.defaultdict(dict)
        return nscache[self.namespace]


if __name__ == '__main__':
    import environment
    import meta
    
    # create a Beaker container type called "ext:local_session".
    # it will reference the ScopedSession in meta.
    ScopedSessionNamespace.create_session_container("ext:local_session", meta.Session)
    
    # set up a region based on this new container type.
    meta.cache_manager.regions['local_session'] ={'type':'ext:local_session'}
    
    from model import Person
    
    # query to load Person by name, with criterion
    # of "person 10"
    q = meta.Session.query(Person).\
                    options(meta.FromCache("local_session", "by_name")).\
                    filter(Person.name=="person 10")
                    
    # load from DB
    person10 = q.one()
    
    # next call, the query is cached.
    person10 = q.one()

    # clear out the Session.  The "_beaker_cache" dictionary
    # disappears with it.
    meta.Session.remove()
    
    # query calls from DB again
    person10 = q.one()
    
    # identity is preserved - person10 is the *same* object that's
    # ultimately inside the cache.   So it is safe to manipulate
    # the not-queried-for attributes of objects when using such a 
    # cache without the need to invalidate - however, any change 
    # that would change the results of a cached query, such as 
    # inserts, deletes, or modification to attributes that are 
    # part of query criterion, still require careful invalidation.
    from meta import _get_cache_parameters
    cache, key = _get_cache_parameters(q)
    assert person10 is cache.get(key)[0]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.