import caching_query from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base from beaker import cache import os # Beaker CacheManager. A home base for cache configurations. cache_manager = cache.CacheManager() # scoped_session. Apply our custom CachingQuery class to it, # using a callable that will associate the cache_manager # with the Query. Session = scoped_session( sessionmaker( query_cls=caching_query.query_callable(cache_manager) ) ) # global declarative base class. Base = declarative_base() root = "./beaker_data/" if not os.path.exists(root): raw_input("Will create datafiles in %r.\n" "To reset the cache + database, delete this directory.\n" "Press enter to continue.\n" % root ) os.makedirs(root)
from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base from dogpile.cache.region import make_region import os import md5 # dogpile cache regions. A home base for cache configurations. regions = {} # scoped_session. Apply our custom CachingQuery class to it, # using a callable that will associate the dictionary # of regions with the Query. Session = scoped_session( sessionmaker( query_cls=caching_query.query_callable(regions) ) ) # global declarative base class. Base = declarative_base() root = "./dogpile_data/" if not os.path.exists(root): raw_input("Will create datafiles in %r.\n" "To reset the cache + database, delete this directory.\n" "Press enter to continue.\n" % root ) os.makedirs(root)
if config['DBCACHE_SVR']: import dogpile.cache import caching_query cache_region = dogpile.cache.make_region() cache_settings = { "cache.redis.backend": "dogpile.cache.redis", "cache.redis.arguments.host": config['DBCACHE_SVR'], "cache.redis.arguments.port": 6379, } cache_region.configure_from_config(cache_settings, "cache.redis.") cache_regions = {"default": cache_region} FromCache = caching_query.FromCache DB_Session = scoped_session(sessionmaker( bind=engine, query_cls=caching_query.query_callable(cache_regions))) else: class FromCache(MapperOption): pass DB_Session = scoped_session(sessionmaker(bind=engine)) # 考虑更灵活的login机制,如login_required("student","teacher") 可以只让学生和老师登陆 def login_required(user_type="any"): """Decorate methods with this to require that the user be logged in.""" def decorator(method): @functools.wraps(method) def wrapper(self, *args, **kwargs): if not self.current_user:
""" import caching_query from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base from beaker import cache import os # Beaker CacheManager. A home base for cache configurations. cache_manager = cache.CacheManager() # scoped_session. Apply our custom CachingQuery class to it, # using a callable that will associate the cache_manager # with the Query. Session = scoped_session( sessionmaker(query_cls=caching_query.query_callable(cache_manager))) # global declarative base class. Base = declarative_base() root = "./beaker_data/" if not os.path.exists(root): raw_input("Will create datafiles in %r.\n" "To reset the cache + database, delete this directory.\n" "Press enter to continue.\n" % root) os.makedirs(root) dbfile = os.path.join(root, "beaker_demo.db") engine = create_engine('sqlite:///%s' % dbfile, echo=True) Session.configure(bind=engine)
import os from hashlib import md5 import sys py2k = sys.version_info < (3, 0) if py2k: input = raw_input # dogpile cache regions. A home base for cache configurations. regions = {} # scoped_session. Apply our custom CachingQuery class to it, # using a callable that will associate the dictionary # of regions with the Query. Session = scoped_session( sessionmaker(query_cls=caching_query.query_callable(regions))) # global declarative base class. Base = declarative_base() root = "./dogpile_data/" if not os.path.exists(root): input("Will create datafiles in %r.\n" "To reset the cache + database, delete this directory.\n" "Press enter to continue.\n" % root) os.makedirs(root) dbfile = os.path.join(root, "dogpile_demo.db") engine = create_engine('sqlite:///%s' % dbfile, echo=True) Session.configure(bind=engine)
from sqlalchemy.orm import backref from sqlalchemy.orm import joinedload import sqlalchemy as sa from caching_query import FromCache from caching_query import RelationshipCache from caching_query import query_callable from dogpile.cache import make_region import hashlib cache_region = make_region() regions = {"default": cache_region} DBSession = scoped_session(sessionmaker(query_cls=query_callable(regions))) def md5_key_mangler(key): """Receive cache keys as long concatenated strings; distill them into an md5 hash. """ d = hashlib.md5(key.encode('utf-8')) return d.hexdigest() class BaseModel(object): @declared_attr def pk(self): return sa.Column(sa.Integer, primary_key=True)