Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
dogpile advice
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jonathan Vanasco  
View profile  
 More options Aug 31 2012, 11:23 am
From: Jonathan Vanasco <jonat...@findmeon.com>
Date: Fri, 31 Aug 2012 08:23:46 -0700 (PDT)
Local: Fri, Aug 31 2012 11:23 am
Subject: dogpile advice
hi all

so i have code that works , i just want to talk about it with other
users and see if this is a good strategy / could be improved.

i had to refactor the caching code to split it into more manageable
chunks and integrate it into the app-config for different environments

this is what came of it...

-   my app has an internal caching api.  it looks like this:
    - - - - - - - - - -
    app.lib.caching
    app.lib.caching.utils ( dogpile setup )
    app.lib.caching.section_a
    app.lib.caching.section_b
    - - - - - - - - - -

- app.lib.caching.utils
    - - - - - - - - - -
    from dogpile.cache import make_region

    # set the initial regions to None
    # we'll update this during application setup
    #
    region_name2id = None

    def initialize_dogpile( config , settings ):
        """ called during app setup
            ensures we have a data directory for file based caches
        """
        customized= { 'data_dir' : settings['data_dir'] }
        mkdir_p( "%(data_dir)s/dogpile" % customized )
        cache_opts = {
            'cache.name2id.backend': 'dogpile.cache.dbm',
            'cache.name2id.expiration_time': '86400',
            'cache.name2id.arguments.filename': '%(data_dir)s/dogpile/
name2id' % customized,
        }
        global region_name2id
        region_name2id = make_region(key_mangler=str)
        region_name2id.configure_from_config( cache_opts ,
'cache.name2id.' )
    - - - - - - - - - -

- app.lib.caching.section_a
    - - - - - - - - - -
    from . import utils

    def user_id_to_username( user_id , dbSession=None , request=None ,
can_use_cache=True  ):
        """ wraps get_user_data
            we stuff the decorator into an 'inner' function, because
the region does not exist until application setup
        """

        @utils.region_name2id.cache_on_arguments()
        def inner( user_id , dbSession , request , can_use_cache ):
            user= get_user_data( user_id , dbSession=dbSession ,
request=request , can_use_cache=can_use_cache )
            if user:
                return user['username']
            return None

        return inner( user_id , dbSession , request , can_use_cache )

    def get_user_data( user_id , dbSession=None , request=None ,
can_use_cache=True ):
        """ gets the user row.
            this will optionally pull from the cache.
            the inner function _sharedcache is called when cached data
is ok...
            ...otherwise we directly query the database.
        """

        def _query(user_id=None):
            user_id = int(user_id)
            useraccount = dbSession.query( model.core.Useraccount.id )
\
                .filter( model.core.Useraccount.id == user_id )\
                .first()
            if useraccount:
                return useraccount[0]
            return None

        @utils.region_name2id.cache_on_arguments()
        def _sharedcache(user_id):
            return _query(user_id)

        if can_use_cache:
            return _sharedcache(user_id)
        return _query(user_id)
    - - - - - - - - - -

generally, i'm wondering if this was the best strategy.

1- I couldn't think-through another strategy of having the cache
regions both configurable by the app config
2- I couldn't think of another way to allow for the decorators to be
used without wrapping them in an inner function.
3- I'm going to assume that the usage of the inner function is giving
me a performance hit, but it's probably really small and not worth
caring about. right?  { i've been avoiding looking into this from fear
of thinking we might need to look at optimizing it }

any feedback ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »