Google App Engine中的树脂会话

时间:2010-12-12 18:46:55

标签: php google-app-engine session

我正在使用Resin开发GAE,似乎我在生产站点上的PHP会话是短暂的并且没有得到更新(即,提出请求似乎并没有增加它的到期时间)。本地很好,只要我不关闭标签,会话就会持续。

这个指针?我的用户因为经常被踢而感到沮丧:(

1 个答案:

答案 0 :(得分:1)

我认为代码是最好的教程:)

// global mem cache service handle
$MEM_CACHE_SERVICE = NULL;
// table to store session like information
$MY_SESSION_TABLE = array();

function load_mcache($key) {
    global $MEM_CACHE_SERVICE;
    if (!$MEM_CACHE_SERVICE) {
        import com.google.appengine.api.memcache.MemcacheServiceFactory;
        import com.google.appengine.api.memcache.Expiration;
        $MEM_CACHE_SERVICE = MemcacheServiceFactory::getMemcacheService();
    }
    return $MEM_CACHE_SERVICE->get($key);
}

function save_mcache($key, $value, $cache_time) {
    global $MEM_CACHE_SERVICE;
    if (!$MEM_CACHE_SERVICE) {
        import com.google.appengine.api.memcache.MemcacheServiceFactory;
        import com.google.appengine.api.memcache.Expiration;
        $MEM_CACHE_SERVICE = MemcacheServiceFactory::getMemcacheService();
    }
    $expiration = Expiration::byDeltaSeconds($cache_time);
    return $MEM_CACHE_SERVICE->put($key, $value, $expiration);
}

// unserializing array from mem cache
// if nothing found like first time and after a minute, then add key to the table
if (!($MY_SESSION_TABLE = unserialize(load_mcache($_REQUEST['JSESSIONID'])))) {
    // save something to cache on first page load because we didnt have anything
    $MY_SESSION_TABLE['key1'] = date('m/d/Y H:i:s');
    // using jsessionid as a mem cache key, serializing array and setting cache time to one minute
    save_mcache($_REQUEST['JSESSIONID'], serialize($MY_SESSION_TABLE), 60);
}

// now my session table is available for a minute until its initialized again
print_r($MY_SESSION_TABLE);

现在为了正确的会话功能,你需要添加set和get方法,或者更好的是一个小类来处理它。对类的抽象很少,您可以选择在不同的Web应用程序场景中使用相同库的会话机制。

相关问题