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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵