php - Zend - 如何在 session 表上启用缓存(元数据)?

标签 php zend-framework session cache-control

我正在使用 Zend_Session_SaveHandler_DbTable 将 session 存储在表中

我的分析器告诉我,在每个页面请求 zend 都会执行以下操作:

# 查询时间

(1) 连接 0.0032038688659668

(2) 描述 session 0.0041539669036865

(3) SELECT session.* FROM session WHERE (((session.session_id = '7nnan8ltd6h64sigs6dlkicvh0 ' AND session.save_path = '' AND session.name = 'PHPSESSID'))) 0.00057697296142578

总时间:0.008秒

当我对其他表进行查询时,zend DESCRIBE 一次(第一次访问该表时),然后如果我刷新页面,它只会执行没有描述的查询,在 session 表上,它会在每个页面上 DESCRIBE (因为我使用身份验证...)

如何在 session 表上仅缓存元数据

我目前正在使用这个

class Gestionale_Application_Resource_Cache extends Zend_Application_Resource_ResourceAbstract{
public function init ()
{
    $options = $this->getOptions();

    // Get a Zend_Cache_Core object

    //valori che vengono presi dal file di configurazione
    $cache = Zend_Cache::factory(
        $options['frontEnd'],
        $options['backEnd'],
        $options['frontEndOptions'],
        $options['backEndOptions']);
    Zend_Registry::set('cache', $cache);

    Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);//per mettere in cache la meta-info
    return $cache;
}

这是我的配置文件

...
;cache stuff
resources.cache.frontEnd = core 
resources.cache.backEnd = file 
resources.cache.frontEndOptions.lifetime = 1200 ; in secondi
resources.cache.frontEndOptions.automatic_serialization = true 
resources.cache.backEndOptions.lifetime = 3600 ; in secondi
resources.cache.backEndOptions.cache_dir = APPLICATION_PATH "/../cache"
pluginPaths.Gestionale_Application_Resource = APPLICATION_PATH "/../library/Gestionale/Application/Resource"  
;;fine cache stuff

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.db.params.charset = "utf8"
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "gestionale"
resources.db.isDefaultTableAdapter = true
autoloaderNamespaces[] = "Gestionale_";serve per caricare il plugin di sotto quando si usa anche ZFdebug
resources.frontController.plugins.acl = "Gestionale_Controller_Plugin_Acl"
resources.db.params.profiler = true
...

这是我的 session 表

CREATE TABLE IF NOT EXISTS `session` (
  `session_id` char(32) NOT NULL,
  `save_path` varchar(32) NOT NULL,
  `name` varchar(32) NOT NULL DEFAULT '',
  `modified` int(11) DEFAULT NULL,
  `lifetime` int(11) DEFAULT NULL,
  `session_data` text,
  PRIMARY KEY (`session_id`,`save_path`,`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

谢谢:D

最佳答案

无论您在 Bootstrap 还是配置文件中初始化 session 保存处理程序,请确保首先调用 Zend_Db_Table_Abstract::setDefaultMetadataCache()。

要在配置文件中指定它,请将 session 配置放在 ;;fine cache stuff 行之后:

...
;cache stuff
resources.cache.frontEnd = core 
resources.cache.backEnd = file 
resources.cache.frontEndOptions.lifetime = 1200 ; in secondi
resources.cache.frontEndOptions.automatic_serialization = true 
resources.cache.backEndOptions.lifetime = 3600 ; in secondi
resources.cache.backEndOptions.cache_dir = APPLICATION_PATH "/../cache"
pluginPaths.Gestionale_Application_Resource = APPLICATION_PATH "/../library/Gestionale/Application/Resource"  
;;fine cache stuff

resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
resources.session.saveHandler.options.name = "session"
resources.session.saveHandler.options.primary[] = "session_id"
resources.session.saveHandler.options.primary[] = "save_path"
resources.session.saveHandler.options.primary[] = "name"
resources.session.saveHandler.options.primaryAssignment[] = "sessionId"
resources.session.saveHandler.options.primaryAssignment[] = "sessionSavePath"
resources.session.saveHandler.options.primaryAssignment[] = "sessionName"
resources.session.saveHandler.options.modifiedColumn = "modified"
resources.session.saveHandler.options.dataColumn = "session_data"
resources.session.saveHandler.options.lifetimeColumn = "lifetime"

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.db.params.charset = "utf8"
...

或者,如果您不想依赖它们在配置文件中的顺序,您可以向引导类添加一个 _initSession() 方法,专门以正确的顺序加载它们:

protected function _initSession()
{
    $this->bootstrap('cache');
    $this->bootstrap('session');
}

关于php - Zend - 如何在 session 表上启用缓存(元数据)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5271018/

相关文章:

php - 使用$ _POST值的简单“插入表”

php - 一旦表单对象已经创建,如何在特定位置添加 zend 表单元素?

jquery - 在ajax请求中返回zend paginator对象,获取错误的url

javascript - 如何记住表单提交时的切换状态 - 在 session 变量中显示/隐藏

php - 如何格式化与我的旧 Php Soap 服务相同的 Yii2 Soap 服务器响应

php - PHP 基于 token 的身份验证

php - 创建数组而不是重复函数

zend-framework - zend 模块模型

java - GWT session 管理

java - 如何将请求分派(dispatch)回发送请求的页面?