# HG changeset patch # User Marcin Kuzminski # Date 2010-10-12 14:39:53 # Node ID c1c1cf772337cd33dc3351d2436f794c8ade4540 # Parent 101e07f82f2286e35e7b3235de0cc89d4a9a9a92 moved out sqlalchemy cache from meta to the config files. added caching query for permissions. diff --git a/development.ini b/development.ini --- a/development.ini +++ b/development.ini @@ -1,6 +1,6 @@ ################################################################################ ################################################################################ -# rhodecode - Pylons environment configuration # +# rhodecode - Pylons environment configuration # # # # The %(here)s variable will be replaced with the parent directory of this file# ################################################################################ @@ -10,7 +10,7 @@ debug = true ################################################################################ ## Uncomment and replace with the address which should receive ## ## any error reports after application crash ## -## Additionally those settings will be used by rhodecode mailing system ## +## Additionally those settings will be used by rhodecode mailing system ## ################################################################################ #email_to = admin@localhost #error_email_from = paste_error@localhost @@ -49,14 +49,25 @@ cache_dir = %(here)s/data #################################### beaker.cache.data_dir=/%(here)s/data/cache/data beaker.cache.lock_dir=/%(here)s/data/cache/lock -beaker.cache.regions=super_short_term,short_term,long_term +beaker.cache.regions=super_short_term,short_term,long_term,sql_cache_short,sql_cache_med,sql_cache_long beaker.cache.long_term.type=memory beaker.cache.long_term.expire=36000 + beaker.cache.short_term.type=memory beaker.cache.short_term.expire=60 + beaker.cache.super_short_term.type=memory beaker.cache.super_short_term.expire=10 +beaker.cache.sql_cache_short.type=memory +beaker.cache.sql_cache_short.expire=5 + +beaker.cache.sql_cache_med.type=memory +beaker.cache.sql_cache_med.expire=360 + +beaker.cache.sql_cache_long.type=file +beaker.cache.sql_cache_long.expire=3600 + #################################### ### BEAKER SESSION #### #################################### diff --git a/production.ini b/production.ini --- a/production.ini +++ b/production.ini @@ -1,6 +1,6 @@ ################################################################################ ################################################################################ -# rhodecode - Pylons environment configuration # +# rhodecode - Pylons environment configuration # # # # The %(here)s variable will be replaced with the parent directory of this file# ################################################################################ @@ -9,8 +9,8 @@ debug = true ################################################################################ ## Uncomment and replace with the address which should receive ## -## any error reports after application crash ## -## Additionally those settings will be used by rhodecode mailing system ## +## any error reports after application crash ## +## Additionally those settings will be used by rhodecode mailing system ## ################################################################################ #email_to = admin@localhost #error_email_from = paste_error@localhost @@ -49,14 +49,25 @@ cache_dir = %(here)s/data #################################### beaker.cache.data_dir=/%(here)s/data/cache/data beaker.cache.lock_dir=/%(here)s/data/cache/lock -beaker.cache.regions=super_short_term,short_term,long_term +beaker.cache.regions=super_short_term,short_term,long_term,sql_cache_short,sql_cache_med,sql_cache_long beaker.cache.long_term.type=memory beaker.cache.long_term.expire=36000 + beaker.cache.short_term.type=memory beaker.cache.short_term.expire=60 + beaker.cache.super_short_term.type=memory beaker.cache.super_short_term.expire=10 +beaker.cache.sql_cache_short.type=memory +beaker.cache.sql_cache_short.expire=5 + +beaker.cache.sql_cache_med.type=memory +beaker.cache.sql_cache_med.expire=360 + +beaker.cache.sql_cache_long.type=file +beaker.cache.sql_cache_long.expire=3600 + #################################### ### BEAKER SESSION #### #################################### diff --git a/rhodecode/config/deployment.ini_tmpl b/rhodecode/config/deployment.ini_tmpl --- a/rhodecode/config/deployment.ini_tmpl +++ b/rhodecode/config/deployment.ini_tmpl @@ -50,14 +50,25 @@ app_instance_uuid = ${app_instance_uuid} #################################### beaker.cache.data_dir=/%(here)s/data/cache/data beaker.cache.lock_dir=/%(here)s/data/cache/lock -beaker.cache.regions=super_short_term,short_term,long_term +beaker.cache.regions=super_short_term,short_term,long_term,sql_cache_short,sql_cache_med,sql_cache_long beaker.cache.long_term.type=memory beaker.cache.long_term.expire=36000 + beaker.cache.short_term.type=memory beaker.cache.short_term.expire=60 + beaker.cache.super_short_term.type=memory beaker.cache.super_short_term.expire=10 +beaker.cache.sql_cache_short.type=memory +beaker.cache.sql_cache_short.expire=5 + +beaker.cache.sql_cache_med.type=memory +beaker.cache.sql_cache_med.expire=360 + +beaker.cache.sql_cache_long.type=file +beaker.cache.sql_cache_long.expire=3600 + #################################### ### BEAKER SESSION #### #################################### diff --git a/rhodecode/lib/auth.py b/rhodecode/lib/auth.py --- a/rhodecode/lib/auth.py +++ b/rhodecode/lib/auth.py @@ -27,6 +27,7 @@ from pylons import config, session, url, from pylons.controllers.util import abort, redirect from rhodecode.lib.utils import get_repo_slug from rhodecode.model import meta +from rhodecode.model.caching_query import FromCache from rhodecode.model.db import User, RepoToPerm, Repository, Permission, \ UserToPerm from sqlalchemy.exc import OperationalError @@ -141,7 +142,9 @@ def fill_data(user): @param user: """ sa = meta.Session - dbuser = sa.query(User).get(user.user_id) + dbuser = sa.query(User).options(FromCache('sql_cache_short', + 'getuser_%s' % user.user_id))\ + .get(user.user_id) if dbuser: user.username = dbuser.username user.is_admin = dbuser.admin @@ -166,11 +169,14 @@ def fill_perms(user): #=========================================================================== # fetch default permissions #=========================================================================== + default_user = sa.query(User)\ + .options(FromCache('sql_cache_short','getuser_%s' % 'default'))\ + .filter(User.username == 'default').scalar() + default_perms = sa.query(RepoToPerm, Repository, Permission)\ .join((Repository, RepoToPerm.repository_id == Repository.repo_id))\ .join((Permission, RepoToPerm.permission_id == Permission.permission_id))\ - .filter(RepoToPerm.user == sa.query(User).filter(User.username == - 'default').scalar()).all() + .filter(RepoToPerm.user == default_user).all() if user.is_admin: #======================================================================= diff --git a/rhodecode/model/caching_query.py b/rhodecode/model/caching_query.py --- a/rhodecode/model/caching_query.py +++ b/rhodecode/model/caching_query.py @@ -18,9 +18,11 @@ The rest of what's here are standard SQL Beaker constructs. """ +from beaker.exceptions import BeakerException from sqlalchemy.orm.interfaces import MapperOption from sqlalchemy.orm.query import Query from sqlalchemy.sql import visitors +import beaker class CachingQuery(Query): """A Query subclass which optionally loads full results from a Beaker @@ -105,7 +107,13 @@ def query_callable(manager): def query(*arg, **kw): return CachingQuery(manager, *arg, **kw) return query - + +def get_cache_region(name, region): + if region not in beaker.cache.cache_regions: + raise BeakerException('Cache region not configured: %s' % region) + kw = beaker.cache.cache_regions[region] + return beaker.cache.Cache._get_cache(name, kw) + def _get_cache_parameters(query): """For a query with cache_region and cache_namespace configured, return the correspoinding Cache instance and cache key, based @@ -125,8 +133,8 @@ def _get_cache_parameters(query): cache_key = " ".join([str(x) for x in args]) # get cache - cache = query.cache_manager.get_cache_region(namespace, region) - + #cache = query.cache_manager.get_cache_region(namespace, region) + cache = get_cache_region(namespace, region) # optional - hash the cache_key too for consistent length # import uuid # cache_key= str(uuid.uuid5(uuid.NAMESPACE_DNS, cache_key)) diff --git a/rhodecode/model/meta.py b/rhodecode/model/meta.py --- a/rhodecode/model/meta.py +++ b/rhodecode/model/meta.py @@ -28,36 +28,6 @@ Base = declarative_base() #=============================================================================== # CACHE OPTIONS #=============================================================================== -cache_base = jn(dn(dn(dn(abspath(__file__)))), 'data') -cache_dir = jn(cache_base, 'cache') - -if not os.path.isdir(cache_base): - os.mkdir(cache_base) - -if not os.path.isdir(cache_dir): - os.mkdir(cache_dir) -# set start_time to current time -# to re-cache everything -# upon application startup -start_time = time.time() -# configure the "sqlalchemy" cache region. -cache_manager.regions['sql_cache_short'] = { - 'type':'memory', - 'data_dir':cache_dir, - 'expire':10, - 'start_time':start_time - } -cache_manager.regions['sql_cache_med'] = { - 'type':'memory', - 'data_dir':cache_dir, - 'expire':360, - 'start_time':start_time - } -cache_manager.regions['sql_cache_long'] = { - 'type':'file', - 'data_dir':cache_dir, - 'expire':3600, - 'start_time':start_time - } +#Configured globally in .ini files #to use cache use this in query #.options(FromCache("sqlalchemy_cache_type", "cachekey"))