duanjun7801 2013-07-05 20:29
浏览 68
已采纳

Phalcon:在会话中存储配置

Brand new to Phalcon (and frameworks in general) so forgive my n00b-ness.

I want to load my config in my bootstrap file, then have it stored in the session (so it only needs to be loaded once, and accessible globally).

I've got my session being created, and I'm successfully loading my configuration info into $Config. How do I store $Config in the session though? Since I'm not in a controller I can't use $this->session. It appears the only way is to pull the session out of the DI, add the $config property, then re-set the DI's session property:

$DI->setShared('session', function(){
    $session = new Phalcon\Session\Adapter\Files();
    if(session_status() == PHP_SESSION_NONE)
        $session->start();
    return $session;
});

$Config = require '../app/config/config.php';
$Session = $DI->get('session');
$Session->config = $Config;
$DI->setShared('session',$Session);

Though that seems like a pretty inefficient way to store something in the session in the bootstrap. Is this the only way to do it or am I missing some hidden functionality? I suppose I could create the session manually (rather than in the anonymous function), set 'config', then store it with $DI->setShared().

  • 写回答

2条回答 默认 最新

  • dtkz3186 2013-07-31 11:32
    关注

    Are you trying to speed up your app by only loading the config once? If so have you profiled the app to make sure it is actually a bottle neck?

    I have a 55 line config file and using xdebug and qcachegrind I can see that loading the config file takes 0.04% of my overall page load. There are probably easier savings to be had in my case!

    Are you using opcode caching? I used APC for years very successfully but recently changed in favour of opcache due to some intermittent problems (it is standard in php 5.5) In my experience opcode caching can give a 50% speed increase, ymmv

    If you've done that and its still not fast enough, then I would agree with the others and say that storing the config in a session is not a great idea as every visitor would have their own, memory usage may also be an issue if you have a lot of users, or if your sessions are stored in files then you might be swapping one config file for hundreds!

    If you think the cache might be worth trying, something like this : adapted from the manual :

    //Cache data for one hour
    $frontCache = new Phalcon\Cache\Frontend\Data(array(
        "lifetime" => 3600
    ));
    
    // Create the component that will cache "Data" to a "Memcached" backend
    // Memcached connection settings
    $cache = new Phalcon\Cache\Backend\Memcache($frontCache, array(
        "host" => "localhost",
        "port" => "11211"
    ));
    
    // Try to get cached records
    $cacheKey = 'site-config';
    $config    = $cache->get($cacheKey);
    if ($config === null) {
        $config = require '../app/config/config.php';
    
        // Store it in the cache
        $cache->save($cacheKey, $config);       
    }
    

    But I think you time will likely be better spent elsewhere in the quest for speed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛