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

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.

I found the Vokuro sample application a really useful way to learn about how a Phalcon app is structured. If you are trying to make the config available within your controller code then using dependency injection might be what you need, you may not need to store the config in session as well. Good luck on your journey.