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 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄