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条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源