Ejemplo n.º 1
0
 public function testStats()
 {
     $stats = new FilecacheStatisticsTracker(vfsStream::url('cacheDir'));
     $manager = new FilecacheStatisticsManager(vfsStream::url('cacheDir'));
     $this->cache->setStatisticsTracker($stats);
     $this->cache->get('test');
     $this->cache->set('test', 'testing', 300);
     $this->cache->get('test');
     $data = $manager->getStatistics();
     $this->assertEquals(array('File cache'), array_keys($data));
     $numbers = $data['File cache']->getFormattedArray();
     $this->assertEquals(array('Hits' => 1, 'Misses' => 1, 'Helpfulness' => '50.00'), $numbers);
 }
 public function register(Application $app)
 {
     // defaults (these can be overridden in the container)
     $app['be_cache.type'] = 'memcache';
     $app['be_cache.host'] = '127.0.0.1';
     $app['be_cache.port'] = '11211';
     $app['be_cache.stats'] = true;
     $app['be_cache.prefix'] = '';
     // register the client
     $app['be_cache.client'] = $app->share(function () use($app) {
         $client = null;
         switch (strtolower($app['be_cache.type'])) {
             default:
                 throw new \Exception('Invalid be_cache.type');
                 break;
             case 'file':
             case 'filecache':
                 if (!$app['be_cache.path'] || !is_writable($app['be_cache.path'])) {
                     throw new \Exception('$app[\'be_cache.path\'] must be writable.');
                 }
                 $client = new FilecacheClient($app['be_cache.path']);
                 if ($app['be_cache.stats']) {
                     $stats = new FilecacheStatisticsTracker($app['be_cache.path']);
                     $client->setStatisticsTracker($stats);
                 }
                 break;
             case 'memcache':
                 $memcache = new \Memcache();
                 $memcache->addserver($app['be_cache.host'], $app['be_cache.port']);
                 $client = new MemcacheClient($memcache);
                 break;
             case 'apc':
                 $client = new APCClient();
                 break;
         }
         return $client;
     });
     //register the service
     $app['be_cache'] = $app->share(function () use($app) {
         $cache = new Cache($app['be_cache.client']);
         $cache->setPrefix($app['be_cache.prefix']);
         return $cache;
     });
 }