The Cache layer of Lithium inherits from the common Adaptable class, which provides the generic configuration setting & retrieval logic, as well as the logic required to locate & instantiate the proper adapter class. In most cases, you will configure various named cache configurations in your bootstrap process, which will then be available to you in all other parts of your application. A simple example configuration: {{{Cache::config(array( 'local' => array('adapter' => 'Apc'), 'distributed' => array( 'adapter' => 'Memcached', 'host' => '127.0.0.1:11211', ), 'default' => array('adapter' => 'File') ));}}} Each adapter provides a consistent interface for the basic cache operations of write, read, delete and clear, which can be used interchangeably between all adapters. Some adapters may provide additional methods that are not consistently available across other adapters. To make use of these, it is always possible to call: {{{Cache::adapter('named-configuration')->methodName($argument);}}} This allows a very wide range of flexibility, at the cost of portability. Some cache adapters (e.g. File) do _not_ provide the functionality for increment/decrement. Additionally, some cache adapters support multi-key operations for write, read and delete — please see the individual documentation for cache adapters and the operations that they support.
See also: lithium\core\Adaptable
See also: lithium\storage\cache\adapter
Inheritance: extends lithium\core\Adaptable
 public function testExpiresQuick()
 {
     Cache::write($this->cachedName, 'foo', 'bar', '+5 second');
     $this->assertEqual('bar', Cache::read($this->cachedName, 'foo'));
     sleep(10);
     $this->assertNull(Cache::read($this->cachedName, 'foo'));
 }
Example #2
0
 /**
  * This renders a a block.
  *
  *
  * @param string $position The block position identifier
  * @param array $options
  * @return string HTML code for the menu
  */
 public function render($position = null, $data = array(), $options = array())
 {
     $defaults = array('cache' => false, 'wrapperId' => false);
     $options += $defaults;
     if (empty($position) || !is_string($position)) {
         return '';
     }
     // set the cache key for the menu
     $cache_key = 'li3b_blocks.' . $position;
     $blocks = false;
     // if told to use the block content from cache
     if (!empty($options['cache'])) {
         $blocks = Cache::read('default', $cache_key);
     }
     // if the content hasn't been set in cache or it was empty for some reason, get a fresh copy of its data
     if (empty($blocks)) {
         $blocks = Block::staticBlock($position);
     }
     // if using cache, write the menu data to the cache key
     if (!empty($options['cache'])) {
         Cache::write('default', $cache_key, $blocks, $options['cache']);
     }
     $string = "\n";
     if ($options['wrapperId']) {
         $string .= '<div id="' . $options['wrapperId'] . '">';
     }
     foreach ($blocks as $block) {
         if (isset($block['options']['wrapperId'])) {
             $string .= "\n\t" . '<div id="' . $block['options']['wrapperId'] . '">';
         }
         // Blocks can be very simple and contain all the content in the array.
         if (is_string($block['content'])) {
             $string .= "\n\t\t" . $block['content'];
         }
         // Or, they can point to an element view template. These are essentially like elements, only they can have layout templates as well.
         if (is_array($block['content'])) {
             if (isset($block['content']['template'])) {
                 $elementOptions = isset($block['content']['options']) ? $block['content']['options'] : array();
                 if (isset($block['content']['library'])) {
                     $elementOptions['library'] = $block['content']['library'];
                 }
                 $elementOptions['layout'] = isset($block['content']['layout']) ? $block['content']['layout'] : 'blank';
                 $elementOptions['template'] = $block['content']['template'];
                 $appConfig = Libraries::get(true);
                 $paths = array('layout' => array('{:library}/views/layouts/{:layout}.{:type}.php', $appConfig['path'] . '/views/layouts/{:layout}.{:type}.php'), 'template' => array($appConfig['path'] . '/views/_libraries/' . $elementOptions['library'] . '/blocks/{:template}.{:type}.php', '{:library}/views/blocks/{:template}.{:type}.php', $appConfig['path'] . '/views/blocks/{:template}.{:type}.php'));
                 $View = new View(array('paths' => $paths));
                 $string .= $View->render('all', $data, $elementOptions);
             }
         }
         if (isset($block['options']['wrapperId'])) {
             $string .= "\n\t" . '</div>';
         }
     }
     if ($options['wrapperId']) {
         $string .= '</div>';
     }
     $string .= "\n";
     return $string;
 }
Example #3
0
 /**
  * Tests the correct writing to the cache adapter. In this test we use the
  * "Memory" cache adapter so that we can easily verify the written message.
  */
 public function testWrite()
 {
     $message = "CacheLog test message...";
     $result = Logger::write('info', $message, array('name' => 'cachelog'));
     $this->assertNotEmpty($result);
     $result = CacheStorage::read('cachelog', 'cachelog_testkey');
     $this->assertEqual($message, $result);
 }
Example #4
0
 public static function _get_cache_name($key, $cache_name = 'default')
 {
     $cache_key = $key;
     $key_md5 = md5($key);
     $config = Cache::config($cache_name);
     if (strtolower($config["adapter"]) == 'file') {
         $cache_key = 'cache/' . substr($key_md5, 0, 1) . '/' . substr($key_md5, 1, 1) . '/' . substr($key_md5, 2, 1) . '/' . $key_md5;
     } else {
         $cache_key .= '_' . substr($key_md5, 0, 2);
     }
     return $cache_key;
 }
 /**
  *
  * Retrieve Stackoverflow tags I am active on, equivalent to the users tags `me` endpoint.
  *
  * @param	int		$min	Stackoverflow `min`
  * @return	object	Returns an array user tags objects.
  */
 public static function me_tags($min = 5)
 {
     if ($cache = Cache::read('memcache', 'stackoverflow_me_tags')) {
         return $cache;
     }
     $service = new Service(array('scheme' => 'https', 'host' => 'api.stackexchange.com', 'timeout' => 2));
     // using `userid` instead of `me` so we don't need to auth
     $data = $service->get('/' . self::API_VERSION . '/users/' . self::USERID . '/tags?key=' . self::KEY . '&order=desc&min=' . $min . '&sort=popular&site=stackoverflow');
     $me_tags = self::response_to_object($data)->items;
     Cache::write('memcache', 'stackoverflow_me_tags', $me_tags, '+2 hours');
     return $me_tags;
 }
Example #6
0
 /**
  * Gets and caches gallery file list.
  *
  * @return void
  */
 protected function _getGalleryFiles()
 {
     $galleryFiles = Cache::read('default', 'gallery_files');
     if ($galleryFiles === false) {
         $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_galleryPath));
         $filesIter = new \RegexIterator($iterator, '/^.+\\.jpg$/i', \RecursiveRegexIterator::GET_MATCH);
         foreach ($filesIter as $file) {
             $this->_galleryFiles[] = str_replace(Libraries::get('app', 'path') . '/webroot', '', $file[0]);
         }
         Cache::write('default', 'gallery_files', serialize($this->_galleryFiles));
     } else {
         $this->_galleryFiles = unserialize($galleryFiles);
     }
 }
Example #7
0
/**
 * Read cache files for assets
 * Returns CSS/Js from Cache if exists
 * @param  object $request
 * @return string          contents of cache file
 */
function readCache($request, array $options)
{
    $http_response_code = null;
    if (!($content = Cache::read('default', $request->url))) {
        // TODO: config switch
        if (!$options['cacheOnly']) {
            return false;
        }
        $http_response_code = 404;
        $content = '404: ' . $options['name'] . ' does not exist';
    }
    // TODO: use lithium constructs
    header('Content-Type: ' . $options['type'], true, $http_response_code);
    echo $content;
    return true;
}
Example #8
0
 /**
  * parses input string with lessc and returns generated css
  *
  * @param string $input complete less code, may include @import statements
  * @param array $options Additional options to control flow of method
  *                       - cache - controls, whether to cache the result
  *                       - cachePath - Where to cache files, defaults to
  *                                     resources/tmp/cache
  * @return string|boolean generated css, false in case of error
  */
 public static function parse($input, array $options = array())
 {
     $defaults = array('header' => true, 'cache' => true, 'cacheConfig' => 'default', 'cacheKey' => md5($input));
     $options += $defaults;
     if ($output = Cache::read($options['cacheConfig'], $options['cacheKey'])) {
         return $output;
     }
     try {
         $less = static::_getLess();
         $output = $less->parse($input);
     } catch (Exception $e) {
         header('Content-Type: text/css', true, 500);
         $output = "/* less compiler exception: {$e->getMessage()} */";
     }
     if ($options['header']) {
         $output = static::_prependHeader($output);
     }
     if ($options['cache']) {
         Cache::read($options['cacheConfig'], $options['cacheKey'], $output);
     }
     return $output;
 }
Example #9
0
 public static function latest()
 {
     $cacheKey = 'posts_latest';
     if ($cached = Cache::read('default', $cacheKey)) {
         return $cached;
     }
     $config = static::$_serviceConfig;
     try {
         $client = new Tumblr($config['consumerKey'], $config['consumerSecret'], $config['token'], $config['tokenSecret']);
         $results = $client->getBlogPosts($config['name'], ['limit' => 2]);
         $results = $results->posts;
     } catch (CurlException $e) {
         return [];
     } catch (Exception $e) {
         return [];
     }
     $results = (new Collection(['data' => $results]))->find(function ($item) {
         return $item->timestamp >= strtotime('-2 months');
     })->each(function ($item) {
         return static::create((array) $item);
     });
     Cache::write('default', $cacheKey, $results, '+1 hour');
     return $results;
 }
Example #10
0
    $cacheKey = 'core.libraries';
    if ($cached = Cache::read('default', $cacheKey)) {
        $cached = (array) $cached + Libraries::cache();
        Libraries::cache($cached);
    }
    $result = $chain->next($self, $params, $chain);
    if ($cached != ($data = Libraries::cache())) {
        Cache::write('default', $cacheKey, $data, '+1 day');
    }
    return $result;
});
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    foreach (Connections::get() as $name) {
        if (!($connection = Connections::get($name)) instanceof Database) {
            continue;
        }
        $connection->applyFilter('describe', function ($self, $params, $chain) use($name) {
            if ($params['fields']) {
                return $chain->next($self, $params, $chain);
            }
            $cacheKey = "data.connections.{$name}.sources.{$params['entity']}.schema";
            return Cache::read('default', $cacheKey, array('write' => function () use($self, $params, $chain) {
                return array('+1 day' => $chain->next($self, $params, $chain));
            }));
        });
    }
    return $chain->next($self, $params, $chain);
});
// setup default memcache cache
Cache::config(array('memcache' => array('adapter' => 'Memcache', 'host' => '127.0.0.1:11211')));
Example #11
0
$version = is_callable('Configure::version') ? Configure::version() : null;
$cakephp20 = $version && version_compare($version, '2.0', '>=');
$cakephp13 = $version && version_compare($version, '1.3', '>=') && !$cakephp20;
if ($cakephp13 || $cakephp20) {
    $cacheRead = function ($key) {
        return Cache::read($key);
    };
    $cacheWrite = function ($key, $value) {
        return Cache::write($key, $value);
    };
} elseif ($lithium) {
    $cacheRead = function ($key) {
        return \lithium\storage\Cache::read('default', $key);
    };
    $cacheWrite = function ($key, $value) {
        return \lithium\storage\Cache::write('default', $key, $value);
    };
}
/*
 * Test for features on this system.
 */
$hasFileinfo = extension_loaded('fileinfo');
$hasImagick = extension_loaded('imagick');
/*
 * Configure the MIME type detection. The detection class is two headed which means it
 * uses both a glob (for matching against file extensions) and a magic adapter (for
 * detecting the type from the content of files). Available `glob` adapters are `Apache`,
 * `Freedesktop`, `Memory` and `Php`. These adapters are also available as a `magic`
 * variant with the addtion of a `Fileinfo` magic adapter. Not all adapters require
 * a file to be passed along with the configuration.
 */
Example #12
0
 public function show($userId = null)
 {
     if (empty($userId)) {
         OauthController::verifyLogged('apontador');
         $userId = Session::read('apontadorId');
     }
     $user = unserialize(Cache::read('default', $userId));
     if (empty($user)) {
         $user = $this->userRepository->get($userId);
         if (!empty($user)) {
             Cache::write("default", $userId, serialize($user), "+1 day");
         }
     }
     $location = new Location();
     $location->load();
     $myUserId = Session::read('apontadorId');
     $iFollow = $this->api->isUserFollowedByMe($myUserId, $userId);
     $title = 'Perfil de ' . $user->getName();
     return compact('title', 'location', 'user', 'iFollow');
 }
Example #13
0
 public function testNonPortableCacheAdapterMethod()
 {
     $config = array('default' => array('adapter' => 'Memory', 'filters' => array()));
     Cache::config($config);
     $result = Cache::config();
     $expected = $config;
     $this->assertEqual($expected, $result);
 }
Example #14
0
<?php

use lithium\storage\Cache;
use app\extensions\core\Constant;
use app\extensions\core\Connections;
Constant::set_define('ISDEV', false);
Constant::set_define('DEMO_LOGIN', false);
Constant::set_define('CACHE', true);
Constant::set_define('SYS_PATH', 'http://coffeesdk.sinaapp.com');
Constant::set_define('WEIXIN_APP_ID', '');
Constant::set_define('WEIXIN_APP_SECRET', '');
// ini_set('session.save_handler', 'memcache');
// ini_set('session.save_path', 'tcp://' . DB_SERVER . ':11221');
Connections::add('default', array('type' => 'database', 'adapter' => 'MySql', 'host' => SAE_MYSQL_HOST_M, 'login' => SAE_MYSQL_USER, 'password' => SAE_MYSQL_PASS, 'database' => SAE_MYSQL_DB, 'port' => SAE_MYSQL_PORT, 'encoding' => 'UTF-8'));
Connections::add('default_read', array('type' => 'database', 'adapter' => 'MySql', 'host' => SAE_MYSQL_HOST_S, 'login' => SAE_MYSQL_USER, 'password' => SAE_MYSQL_PASS, 'database' => SAE_MYSQL_DB, 'port' => SAE_MYSQL_PORT, 'encoding' => 'UTF-8'));
Connections::add('feeds', array('type' => 'database', 'adapter' => 'MySql', 'host' => SAE_MYSQL_HOST_M, 'login' => SAE_MYSQL_USER, 'password' => SAE_MYSQL_PASS, 'database' => SAE_MYSQL_DB, 'port' => SAE_MYSQL_PORT, 'encoding' => 'UTF-8'));
Cache::config(array('default' => array('adapter' => 'Memcache', 'host' => '127.0.0.1:11221')));
Example #15
0
 public function testFileAdapterMultipleStrategies()
 {
     $resources = Libraries::get(true, 'resources');
     $path = "{$resources}/tmp/cache";
     $this->skipIf(!$this->_checkPath(), "{$path} does not have the proper permissions.");
     $config = array('default' => compact('path') + array('adapter' => 'File', 'filters' => array(), 'strategies' => array('Serializer', 'Base64')));
     Cache::config($config);
     $data = array('some' => 'data');
     $time = time();
     $result = Cache::write('default', 'key', $data, "@{$time} +1 minute");
     $this->assertNotEmpty($result);
     $time = $time + 60;
     $result = file_get_contents("{$path}/key");
     $expected = "{:expiry:{$time}}\nYToxOntzOjQ6InNvbWUiO3M6NDoiZGF0YSI7fQ==";
     $this->assertEqual($result, $expected);
     $result = Cache::read('default', 'key');
     $this->assertEqual($data, $result);
     $result = unlink("{$path}/key");
     $this->assertTrue($result);
     $this->assertFileNotExists("{$path}/key");
 }
Example #16
0
 /**
  * This renders a menu for use with Twitter Bootstrap
  * CSS and JS for Twitter Bootstrap style drop down menus.
  *
  * Note: No need to pass class names, etc. unless they are different
  * than what Twitter Bootstrap requires.
  *
  * @param string $name The menu name
  * @param array $options
  * @return string HTML code for the menu
  */
 public function render($name = null, $options = array())
 {
     $defaults = array('cache' => false, 'menuId' => '', 'menuClass' => '', 'activeClass' => 'active');
     $options += $defaults;
     if (empty($name) || !is_string($name)) {
         return '';
     }
     // Get the current URL (false excludes the domain)
     $here = $this->_context->html->here(false);
     // set the cache key for the menu
     $cache_key = empty($name) ? 'li3b_menus.all' : 'li3b_menus.' . $name;
     $menu = false;
     // if told to use the menu from cache (note: filters will not be applied for this call because Menu::staticMenu() should not be called provided there's a copy in cache)
     if (!empty($options['cache'])) {
         $menu = Cache::read('default', $cache_key);
     }
     // if the menu hasn't been set in cache or it was empty for some reason, get a fresh copy of its data
     if (empty($menu)) {
         $menu = Menu::staticMenu($name);
     }
     // if using cache, write the menu data to the cache key
     if (!empty($options['cache'])) {
         Cache::write('default', $cache_key, $menu, $options['cache']);
     }
     // Format the HTML for the menu
     // option for additional custom menu class
     $menuClass = ' ' . $options['menuClass'];
     $activeClassName = ' ' . $options['activeClass'];
     $string = "\n";
     $string .= '<ul class="nav nav-pills ' . $name . '_menu' . $menuClass . '" id="' . $options['menuId'] . '">';
     $string .= "\n";
     if (is_array($menu)) {
         $i = 1;
         $total = count($menu);
         foreach ($menu as $parent) {
             $title = isset($parent['title']) && !empty($parent['title']) ? $parent['title'] : false;
             $url = isset($parent['url']) && !empty($parent['url']) ? $parent['url'] : false;
             $activeIf = isset($parent['activeIf']) && !empty($parent['activeIf']) ? $parent['activeIf'] : array();
             $options = isset($parent['options']) && is_array($parent['options']) ? $parent['options'] : array();
             $sub_items = isset($parent['subItems']) && is_array($parent['subItems']) ? $parent['subItems'] : array();
             if ($parent == 'separator' || $parent == 'spacer' || $parent == 'divider') {
                 $string .= '<li class="divider-vertical"></li>';
             } else {
                 if ($title && $url) {
                     $string .= "\t";
                     $matched_route = false;
                     try {
                         $matched_route = Router::match($url);
                     } catch (\Exception $e) {
                     }
                     // /admin is of course admin_ prefix actions
                     $activeClass = $matched_route == $here || strstr($here, '/admin' . $matched_route) ? $activeClassName : '';
                     // This plus the Router::match() above really needs some love.
                     // Less if statements...Should be some shorter/nicer way to write it.
                     if (!empty($activeIf)) {
                         if (isset($activeIf['library']) && isset($this->_context->request()->params['library'])) {
                             if ($activeIf['library'] == $this->_context->request()->params['library']) {
                                 $activeClass = $activeClassName;
                             }
                         } elseif (isset($this->_context->request()->params['controller']) && (isset($activeIf['controller']) && $activeIf['controller'] == $this->_context->request()->params['controller'])) {
                             $activeClass = $activeClassName;
                         } elseif (isset($activeIf['url']) && $activeIf['url'] == $this->_context->request()->url) {
                             $activeClass = $activeClassName;
                         }
                     }
                     $string .= '<li class="dropdown ' . $activeClass . '">' . $this->_context->html->link($title, $url, $options += array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'));
                     // sub menu items
                     if (count($sub_items) > 0) {
                         $string .= "\n\t";
                         $string .= '<ul class="dropdown-menu">';
                         $string .= "\n";
                         foreach ($sub_items as $child) {
                             // let any of these do it
                             if ($child == 'separator' || $child == 'spacer' || $child == 'divider') {
                                 $string .= '<li class="divider"></li>';
                             } else {
                                 $title = isset($child['title']) && !empty($child['title']) ? $child['title'] : false;
                                 $url = isset($child['url']) && !empty($child['url']) ? $child['url'] : false;
                                 $options = isset($child['options']) && is_array($child['options']) ? $child['options'] : array();
                                 if ($title && $url) {
                                     $string .= "\t\t";
                                     $string .= '<li>' . $this->_context->html->link($title, $url, $options) . '</li>';
                                     $string .= "\n";
                                 }
                             }
                         }
                         $string .= "\t";
                         $string .= '</ul>';
                         $string .= "\n";
                     }
                     $string .= '</li>';
                     $string .= "\n";
                 }
             }
             $i++;
         }
     }
     $string .= '</ul>';
     $string .= "\n";
     return $string;
 }
Example #17
0
         return $chain->next($self, $params, $chain);
     }
     $cacheKey = 'fpc_' . md5($request->url);
     if ($cached = Cache::read('default', $cacheKey)) {
         return $cached;
     }
     $response = $chain->next($self, $params, $chain);
     switch ($request->url) {
         case strpos($request->url, '/bot') === 0:
             $ttl = '+2 minutes';
         case '/':
             $ttl = '+1 hour';
         default:
             $ttl = Cache::PERSIST;
     }
     Cache::write('default', $cacheKey, $response, $ttl);
     return $response;
 });
 Dispatcher::applyFilter('run', function ($self, $params, $chain) {
     $request = $params['request'];
     $response = $chain->next($self, $params, $chain);
     // Cache only HTML responses, JSON responses come from
     // APIs and are most often highly dynamic.
     if (!$request->is('get') || $response->type() !== 'html') {
         return $response;
     }
     $hash = 'W/' . md5(serialize([$response->body, $response->headers, PROJECT_VERSION]));
     $condition = trim($request->get('http:if_none_match'), '"');
     if ($condition === $hash) {
         $response->status(304);
         $response->body = [];
Example #18
0
 * removed post-install, and the cache should be configured with the adapter you plan to use.
 */
$cachePath = Libraries::get(true, 'resources') . '/tmp/cache';
if (!($apcEnabled = Apc::enabled()) && !is_writable($cachePath)) {
    return;
}
/**
 * This configures the default cache, based on whether ot not APC user caching is enabled. If it is
 * not, file caching will be used. Most of this code is for getting you up and running only, and
 * should be replaced with a hard-coded configuration, based on the cache(s) you plan to use.
 */
$default = array('adapter' => 'File', 'strategies' => array('Serializer'));
if ($apcEnabled) {
    $default = array('adapter' => 'Apc');
}
Cache::config(compact('default'));
/**
 * Caches paths for auto-loaded and service-located classes.
 */
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    $key = md5(LITHIUM_APP_PATH) . '.core.libraries';
    if ($cache = Cache::read('default', $key)) {
        $cache = (array) $cache + Libraries::cache();
        Libraries::cache($cache);
    }
    $result = $chain->next($self, $params, $chain);
    if ($cache != Libraries::cache()) {
        Cache::write('default', $key, Libraries::cache(), '+1 day');
    }
    return $result;
});
Example #19
0
 public function show($placeId = null)
 {
     $placeId = isset($this->request->params['placeId']) ? $this->request->params['placeId'] : $placeId;
     if (empty($placeId)) {
         $this->redirect('/');
     }
     $place = unserialize(Cache::read('default', $placeId));
     if (empty($place)) {
         $place = $this->api->getPlace(array('placeid' => $placeId));
         if (!empty($place)) {
             Cache::write("default", $placeId, serialize($place), "+1 day");
         }
     }
     if ($place instanceof Place) {
         $thePlaceId = $placeId;
         $location = new Location();
         $location->load();
         $placeId = $thePlaceId;
         $visitors = $this->api->getVisitors(array('placeid' => $placeId));
         $place->setNumVisitors($visitors->getNumFound());
         $photos = $this->api->getPhotos(array('placeId' => $placeId));
         $place->setNumPhotos(count($photos->getItems()));
         $showCheckin = true;
         $og = new OpenGraph();
         $og->populate($place);
         $abm = new ABMeta();
         $abm->populate($place);
         $meta = implode('', array($og->getMeta(), $abm->getMeta(), "\t<link rel=\"canonical\" href=\"" . $place->getPlaceUrl() . "\" />\n"));
         $abmType = $abm->get('type');
         $title = $place->getName();
         return compact('meta', 'title', 'location', 'abmType', 'numVisitors', 'showCheckin', 'place');
     } else {
         $this->redirect('/');
     }
 }
Example #20
0
 public function testIntegrationFileAdapterMultipleStrategies()
 {
     $directory = new SplFileInfo(Libraries::get(true, 'resources') . "/tmp/cache/");
     $accessible = $directory->isDir() && $directory->isReadable() && $directory->isWritable();
     $message = "{$directory} does not have the proper permissions.";
     $this->skipIf(!$accessible, $message);
     $config = array('default' => array('adapter' => 'File', 'path' => Libraries::get(true, 'resources') . '/tmp/cache', 'filters' => array(), 'strategies' => array('Serializer', 'Base64')));
     Cache::config($config);
     $data = array('some' => 'data');
     $result = Cache::write('default', 'key', $data, '+1 minute');
     $this->assertTrue($result);
     $time = time() + 60;
     $result = file_get_contents(Libraries::get(true, 'resources') . '/tmp/cache/key');
     $expected = "{:expiry:{$time}}\nYToxOntzOjQ6InNvbWUiO3M6NDoiZGF0YSI7fQ==";
     $this->assertEqual($result, $expected);
     $result = Cache::read('default', 'key');
     $this->assertEqual($data, $result);
     $result = unlink(Libraries::get(true, 'resources') . '/tmp/cache/key');
     $this->assertTrue($result);
     $this->assertFalse(file_exists(Libraries::get(true, 'resources') . '/tmp/cache/key'));
 }
Example #21
0
 public static function hasYear($channel, $date)
 {
     $cacheKey = 'li3_bot_log_messages_has_year_' . md5($channel . $date);
     if (($cached = Cache::read('default', $cacheKey)) !== null) {
         return $cached;
     }
     $result = (bool) static::find('count', ['conditions' => ['channel' => $channel, "YEAR(created)" => $date]]);
     if ($date <= date('Y')) {
         // Do not cache future missed years.
         Cache::write('default', $cacheKey, $result, Cache::PERSIST);
     }
     return $result;
 }
Example #22
0
<?php

use lithium\storage\Cache;
use app\extensions\core\Connections;
use app\extensions\core\Constant;
Constant::set_define('ISDEV', true);
Constant::set_define('DEMO_LOGIN', true);
Constant::set_define('CACHE', false);
Constant::set_define('SYS_PATH', 'http://dev.raytrend.oa.com');
Constant::set_define('DEV_SERVER_HOSET', 'localhost');
Constant::set_define('WEIXIN_APP_ID', '');
Constant::set_define('WEIXIN_APP_SECRET', '');
Connections::add('default', array('type' => 'database', 'adapter' => 'MySql', 'host' => 'localhost', 'login' => 'root', 'password' => 'sa', 'database' => 'rwe', 'encoding' => 'UTF-8', 'persistent' => false));
Connections::add('feeds', array('type' => 'database', 'adapter' => 'MySql', 'host' => 'localhost', 'login' => 'root', 'password' => 'sa', 'database' => 'rwe_feeds', 'encoding' => 'UTF-8'));
Cache::config(array('default' => array('adapter' => 'File', 'strategies' => array('Serializer'))));
Example #23
0
    return;
}
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    $cacheKey = 'core.libraries';
    if ($cached = Cache::read('default', $cacheKey)) {
        $cached = (array) $cached + Libraries::cache();
        Libraries::cache($cached);
    }
    $result = $chain->next($self, $params, $chain);
    if ($cached != ($data = Libraries::cache())) {
        Cache::write('default', $cacheKey, $data, '+1 day');
    }
    return $result;
});
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    foreach (Connections::get() as $name) {
        if (!($connection = Connections::get($name)) instanceof Database) {
            continue;
        }
        $connection->applyFilter('describe', function ($self, $params, $chain) use($name) {
            if ($params['fields']) {
                return $chain->next($self, $params, $chain);
            }
            $cacheKey = "data.connections.{$name}.sources.{$params['entity']}.schema";
            return Cache::read('default', $cacheKey, array('write' => function () use($self, $params, $chain) {
                return array('+1 day' => $chain->next($self, $params, $chain));
            }));
        });
    }
    return $chain->next($self, $params, $chain);
});
	public function index($username = false) {
		
		try {
			$flickr = \lithium\data\Connections::get('flickr');	
		} catch (\lithium\core\ConfigException $flickrDebugVars) {
			return $this->set(compact('flickrDebugVars'));
		}
		
		$username = !$username ? 'JacopKane' : $username;
		$flickrDebugVars = $flickr ? array(
				
			'flickr.test.echo' => $flickr->test_echo() ? $flickr->connection->last->response : 'fail',

			'this->userId' => $this->userId = isset($flickr->people_findByUsername(array(
				'username' => $username
			))->user->nsid) ? $flickr->connection->last->response->user->nsid : false,

			'flickr.test.login'	=> $flickr->test_login() ? $flickr->connection->last->response : 'fail',
			
			'permission.default' => $flickr->checkPermission('default') ? 'pass' : 'fail',
			
			'permission.read' => $flickr->checkPermission('read') ? 'pass' : $this->redirect($flickr->getAuthUrl('read')),

			'permission.write' => $flickr->checkPermission('write') ? 'pass' : 'fail',

			'permission.delete' => $flickr->checkPermission('delete') ? 'pass' : 'fail',

			'flickr.people.getPhotos me' => (
				isset($flickr->people_getPhotos(array(
					'user_id' => 'me'
				))->photos->photo) ? $flickr->connection->last->response->photos->photo : 'fail'
			),

			'photoDomain' => $this->photoTest = empty($flickr->connection->last->response->photos->photo) ? false : (
				$flickr->getDomain(array(
					'id'		=> 5723127353,
		            'secret'	=> 9634646433,
		            'server' 	=> 5172,
		            'farm'		=> 6,
		            'size'		=> 'b',
		            'extension'	=> 'jpg'
				), 'photo')
			),

			'photoTest' => $this->photoTest === false ? 'no photos found' : (
				"<img src=\"{$this->photoTest}\" />"
			),

			'flickr.people.getInfo' => $flickr->people_getInfo(array(
				'user_id' => $this->userId
			)) ? $flickr->connection->last->response : 'fail',

			'flickr.favorites.getList' => $flickr->favorites_getList() ? $flickr->connection->last->response : 'fail',

			'api' => array_map(function($method) {
				return $method->_content;
			}, isset($flickr->reflection_getMethods()->methods->method) ? $flickr->reflection_getMethods()->methods->method : array()),

			'cached_methods' => \lithium\storage\Cache::read('default', 'li3_flickr_methods')

		): 'no connection';

		$this->set(compact('flickrDebugVars'));
	}
 public function testIntegrationFileAdapterWrite()
 {
     $config = array('default' => array('adapter' => 'File', 'path' => LITHIUM_APP_PATH . '/resources/tmp/cache', 'filters' => array()));
     Cache::config($config);
     $result = Cache::write('default', 'key', 'value', '+1 minute');
     $this->assertTrue($result);
     $time = time() + 60;
     $result = file_get_contents(LITHIUM_APP_PATH . '/resources/tmp/cache/key');
     $expected = "{:expiry:{$time}}\nvalue";
     $this->assertEqual($result, $expected);
     $result = unlink(LITHIUM_APP_PATH . '/resources/tmp/cache/key');
     $this->assertTrue($result);
     $this->assertFalse(file_exists(LITHIUM_APP_PATH . '/resources/tmp/cache/key'));
 }
Example #26
0
use lithium\storage\Cache;
use lithium\core\Libraries;
use lithium\action\Dispatcher;
use lithium\storage\cache\adapter\Apc;
if (PHP_SAPI === 'cli') {
    return;
}
/**
 * If APC is not available and the cache directory is not writeable, bail out. This block should be
 * removed post-install, and the cache should be configured with the adapter you plan to use.
 */
if (!($apcEnabled = Apc::enabled()) && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache')) {
    return;
}
if ($apcEnabled) {
    $default = array('adapter' => 'lithium\\storage\\cache\\adapter\\Apc');
} else {
    $default = array('adapter' => 'lithium\\storage\\cache\\adapter\\File', 'strategies' => array('Serializer'));
}
Cache::config(compact('default'));
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    if ($cache = Cache::read('default', 'core.libraries')) {
        $cache = (array) $cache + Libraries::cache();
        Libraries::cache($cache);
    }
    $result = $chain->next($self, $params, $chain);
    if ($cache != Libraries::cache()) {
        Cache::write('default', 'core.libraries', Libraries::cache(), '+1 day');
    }
    return $result;
});
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2010, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */
/**
 * This file creates a default cache configuration using the most optimized adapter available, and
 * uses it to provide default caching for high-overhead operations.
 */
use lithium\storage\Cache;
use lithium\core\Libraries;
use lithium\action\Dispatcher;
use lithium\storage\cache\adapter\Apc;
/**
 * If APC is not available and the cache directory is not writeable, bail out.
 */
if (!($apcEnabled = Apc::enabled() && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache'))) {
    return;
}
Cache::config(array('default' => array('adapter' => '\\lithium\\storage\\cache\\adapter\\' . ($apcEnabled ? 'Apc' : 'File'))));
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    if ($cache = Cache::read('default', 'core.libraries')) {
        $cache = (array) unserialize($cache) + Libraries::cache();
        Libraries::cache($cache);
    }
    $result = $chain->next($self, $params, $chain);
    if ($cache != Libraries::cache()) {
        Cache::write('default', 'core.libraries', serialize(Libraries::cache()), '+1 day');
    }
    return $result;
});
Example #28
0
 /**
  * Rebuild asset cache file
  * @param  string $filename The name of the file to cache or test caching on
  * @param  string $content  File contents to be cached
  * @param  array  $options  Options to handle cache length, location, so on.
  */
 private function setCache($filename, $content, $options = array())
 {
     // Create css cache dir if it doesnt exist.
     if (!is_dir($cache_location = CACHE_DIR . "/{$options['location']}")) {
         mkdir($cache_location, 0755, true);
     }
     $defaults = array('length' => '+1 year', 'location' => 'templates');
     $options += $defaults;
     $name_sections = explode('_', $filename);
     $like_files = $name_sections[0];
     // loop thru cache and delete old cache file
     if (!$this->_production && ($handle = opendir($cache_location))) {
         while (false !== ($oldfile = readdir($handle))) {
             if (preg_match("/^{$like_files}/", $oldfile)) {
                 Cache::delete('default', "{$options['location']}/{$oldfile}");
             }
         }
         closedir($handle);
     }
     Cache::write('default', "{$options['location']}/{$filename}", $content, $options['length']);
 }