def index(cls, group_id):
        # confirm participant id in cookie
        c_group_id = request.cookies.get("group_id")
        participant_id = request.cookies.get("participant_id")

        # get group
        group = Group.get_by_id(group_id)

        # if participant is none or login to another group, create new participant in group
        if not participant_id or c_group_id != str(group_id):
            # create new participant todo:consider the case that group is None
            participant = Participant()
            participant.group_key = group.key
            participant_id = participant.put().id()  # save it to datastore

        # create channel
        participant_id_str = str(participant_id)
        cache = GAEMemcachedCache()
        token = cache.get(participant_id_str)
        if token is None:
            token = channel.create_channel(participant_id_str)
            # expiration of channel api token is 2 hour
            # https://developers.google.com/appengine/docs/python/channel/?hl=ja#Python_Tokens_and_security
            cache.set(participant_id_str, token, 3600 * 2)

        # return response
        resp = make_response(render_template('chat.html', token=token, group_name=group.name))

        # set participant_id to cookie
        resp.set_cookie("group_id", str(group_id),  expires=Config.calculate_expiration())
        resp.set_cookie("participant_id", participant_id_str, expires=Config.calculate_expiration())

        return resp
Exemple #2
0
 def __init__(self, session_class=None):
     super(Store, self).__init__(session_class)
     if settings.DATABASE_ENGINE == 'gae':
         self.cache = GAEMemcachedCache(default_timeout=0)
     else:
         server = settings.SESSION_OPTIONS.get('memcached_servers', [])
         self.cache = MemcachedCache(servers, default_timeout=0)
Exemple #3
0
# coding: UTF-8

from flask import g
from flask import redirect
from flask import url_for

from functools import wraps

from werkzeug.contrib.cache import GAEMemcachedCache
cache = GAEMemcachedCache()

def login_required(f):
    """
    redirects to the index page if the user has no session
    """
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            return redirect(url_for('index'))
        return f(*args, **kwargs)
    return decorated_function

def cache_page(timeout=5 * 60, key='view/%s'):
    """
    caches a full page in memcache, takes a timeout in seconds
    which specifies how long the cache should be valid.
    also allows a formatstring to be used as memcache key prefix.

    source:
    http://flask.pocoo.org/docs/patterns/viewdecorators/#caching-decorator
    """
Exemple #4
0
def gaememcached(app, config, args, kwargs):
    kwargs.update(dict(key_prefix=config['CACHE_KEY_PREFIX']))
    return GAEMemcachedCache(*args, **kwargs)
Exemple #5
0
    'null':
    lambda: NullCache(),
    'simple':
    lambda: SimpleCache(ctx.cfg['caching.timeout']),
    'memcached':
    lambda: MemcachedCache([
        x.strip() for x in ctx.cfg['caching.memcached_servers'].split(',')
    ], ctx.cfg['caching.timeout']),
    'filesystem':
    lambda: FileSystemCache(join(ctx.cfg['caching.filesystem_cache_path']),
                            threshold=500,
                            default_timeout=ctx.cfg['caching.timeout']),
    'database':
    lambda: DatabaseCache(ctx.cfg['caching.timeout']),
    'gaememcached':
    lambda: GAEMemcachedCache(ctx.cfg['caching.timeout'])
}


def set_cache():
    """Set and return the cache for the application.  This is called during
    the application setup.  No need to call that afterwards.
    """
    global cache
    cache = CACHE_SYSTEMS[ctx.cfg['caching.system']]()
    return cache


# enable the caching system
set_cache()