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

发布于 2024-10-21 03:34:15 字数 2721 浏览 1 评论 0原文

我使用 Zend_Session_SaveHandler_DbTable 将会话存储在表中,

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

# Query Time

(1) connect 0.0032038688659668

(2) DESCRIBE session 0.0041539669036865

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

总时间:0.008 秒

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

如何在会话表上仅缓存元数据

我目前正在使用这个

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
...

这是我的会话表,

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

i am using Zend_Session_SaveHandler_DbTable to store the session in a table

my profiler tells me that on each page request zend does:

# Query Time

(1) connect 0.0032038688659668

(2) DESCRIBE session 0.0041539669036865

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

Total time : 0.008 sec

when i do queries on other tables, zend DESCRIBEs them once(the first time it access that table), then if i refresh the page it only does the query with no Describe, on the session table it does DESCRIBE on every page (since i use authentication ... )

how can i cache only the metadata on the session table?

i am currently using this

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;
}

this is my config file

...
;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
...

this is my session table

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;

thanks :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2024-10-28 03:34:16

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

要在配置文件中指定它,请将会话配置放在 ;;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');
}

Wherever you are initializing the session save handler, either in the bootstrap or the config file, make sure that you call Zend_Db_Table_Abstract::setDefaultMetadataCache() first.

To specify it in your config file, put the session config after the ;;fine cache stuff line:

...
;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"
...

Or, if you don't want to rely on their order in the config file, you can add an _initSession() method to your bootstrap class that specifically loads them in the correct order:

protected function _initSession()
{
    $this->bootstrap('cache');
    $this->bootstrap('session');
}
泼猴你往哪里跑 2024-10-28 03:34:16

您必须指定名为“dbMetadataCache”的缓存,它将缓存所有表的元数据。

这是使用 APC 作为后端的示例

resources.cachemanager.dbMetadataCache.frontend.name = "Core"
resources.cachemanager.dbMetadataCache.frontend.options.automatic_serialization = 1
resources.cachemanager.dbMetadataCache.frontend.options.caching = 1
resources.cachemanager.dbMetadataCache.backend.name = "Apc"

resources.db.defaultMetadataCache = "dbMetadataCache"

You must specify cache named as 'dbMetadataCache' and it will cache metadata of all tables.

Here is a example using APC as a backend

resources.cachemanager.dbMetadataCache.frontend.name = "Core"
resources.cachemanager.dbMetadataCache.frontend.options.automatic_serialization = 1
resources.cachemanager.dbMetadataCache.frontend.options.caching = 1
resources.cachemanager.dbMetadataCache.backend.name = "Apc"

resources.db.defaultMetadataCache = "dbMetadataCache"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文