changeset 609:c1c1cf772337

moved out sqlalchemy cache from meta to the config files. added caching query for permissions.
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 12 Oct 2010 16:39:53 +0200
parents 101e07f82f22
children b0a411f5ec70
files development.ini production.ini rhodecode/config/deployment.ini_tmpl rhodecode/lib/auth.py rhodecode/model/caching_query.py rhodecode/model/meta.py
diffstat 6 files changed, 62 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/development.ini	Tue Oct 12 13:03:14 2010 +0200
+++ b/development.ini	Tue Oct 12 16:39:53 2010 +0200
@@ -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 @@
 ################################################################################
 ## 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 @@
 ####################################
 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        ####
 ####################################
--- a/production.ini	Tue Oct 12 13:03:14 2010 +0200
+++ b/production.ini	Tue Oct 12 16:39:53 2010 +0200
@@ -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 @@
 ####################################
 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        ####
 ####################################
--- a/rhodecode/config/deployment.ini_tmpl	Tue Oct 12 13:03:14 2010 +0200
+++ b/rhodecode/config/deployment.ini_tmpl	Tue Oct 12 16:39:53 2010 +0200
@@ -50,14 +50,25 @@
 ####################################
 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        ####
 ####################################
--- a/rhodecode/lib/auth.py	Tue Oct 12 13:03:14 2010 +0200
+++ b/rhodecode/lib/auth.py	Tue Oct 12 16:39:53 2010 +0200
@@ -27,6 +27,7 @@
 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 @@
     @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 @@
     #===========================================================================
     # 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:
         #=======================================================================
--- a/rhodecode/model/caching_query.py	Tue Oct 12 13:03:14 2010 +0200
+++ b/rhodecode/model/caching_query.py	Tue Oct 12 16:39:53 2010 +0200
@@ -18,9 +18,11 @@
 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(*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 @@
         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))
--- a/rhodecode/model/meta.py	Tue Oct 12 13:03:14 2010 +0200
+++ b/rhodecode/model/meta.py	Tue Oct 12 16:39:53 2010 +0200
@@ -28,36 +28,6 @@
 #===============================================================================
 # 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"))