Cache objects component March 15, 2006
Posted by rossoft in CakePHP.5 comments
Usage:
$cacheName = “my_controller/$id”;
$data=$this->cache->read($cacheName);
if (!$data) //if isn’t on cache…
{
$this->MyModel->id=$id;
$data=$this->MyModel->read();
$this->cache->write($cacheName,$data,’+1 hour’); //save to
cache and set expiration date. It will be deleted automatically after
expiration even if it isn’t readed anymore
}
Source code
<?php
/*
* Cache Objects component.
* Read and write to cache with “garbage collector”
* It will delete the expired ones automatically even
* if they aren’t accessed.
* You have to define the expiration date at write.
*
* It’s thread safe with mutex.
*
* @author RosSoft
* @version 0.2
* @license MIT
*
*/
define(’CACHE_COMPONENT_INFO_FILENAME’,CACHE .’cache_component_info’ );
define(’CACHE_COMPONENT_INFO_DIR’,'cache_component_objects/’ );
class CacheObjectComponent extends Object
{
/** Mutex variable */
var $mutex;
/** Info of items */
var $info;
/** array of items added / modified */
var $modified;
/** array of items deleted */
var $deleted;
function __construct()
{
$this->modified=array();
$this->deleted=array();
$this->_load_info();
}
function _load_info($lock=true)
{
//Load the cache info file
if (file_exists(CACHE_COMPONENT_INFO_FILENAME))
{
if ($lock)
{
$this->_mutex_lock();
}
$this->info=unserialize(file_get_contents(CACHE_COMPONENT_INFO_FILENAME));
if ($lock)
{
$this->_mutex_unlock();
}
}
else
{
$this->info=array();
}
}
/**
* save the cache info
*/
function _write_info()
{
if (count($this->modified)>0
|| count($this->deleted)>0)
{
$this->_mutex_lock();
$this->_load_info(false);
foreach ($this->modified as $name=>$value)
{
$this->info[$name]=$value;
}
foreach ($this->deleted as $name)
{
unset($this->info[$name]);
}
file_put_contents(CACHE_COMPONENT_INFO_FILENAME,serialize($this->info));
$this->modified=array();
$this->deleted=array();
$this->_mutex_unlock();
}
}
/**
* Locks a mutex
*/
function _mutex_lock()
{
$this->mutex = @fopen(CACHE_COMPONENT_INFO_FILENAME . ‘.lock’, ‘w’);
if ( false == $this->mutex)
{
return false;
}
flock($this->mutex, LOCK_EX);
return true;
}
/**
* Releases a mutex
*/
function _mutex_unlock()
{
// Release write lock.
flock($this->mutex, LOCK_UN);
fclose($this->mutex);
@unlink(CACHE_COMPONENT_INFO_FILENAME . ‘.lock’);
}
function __destruct()
{
//delete expirated files
$names=array_keys($this->info);
foreach ($names as $name)
{
if ($this->expired($name))
{
@unlink(CACHE . CACHE_COMPONENT_INFO_DIR . $name);
unset($this->info[$name]);
$this->deleted[]=$name;
}
}
$this->_write_info();
}
/**
* Read a data from cache file
* @param string $name File path within /tmp/cache to read the file.
* @return mixed The contents of the temporary file. False if expired
*/
function read($name)
{
if ($this->expired($name))
{
return false;
}
else
{
return unserialize(cache(CACHE_COMPONENT_INFO_DIR . $name,null,’+1 year’));
}
}
/**
* Write data to cache file
* @param string $name File path within /tmp/cache to save the file.
* @param mixed $data The data to save to the temporary file.
* @param mixed $expires A valid strtotime string when the data expires.
*/
function write($name,$data,$expires=’+1 day’)
{
$expires = strtotime($expires);
$this->info[$name]=$expires;
$this->modified[$name]=$expires;
cache(CACHE_COMPONENT_INFO_DIR . $name,serialize($data)); //writes to cache
}
/**
* Tests if a cache file has expired
* @return boolean True if expired
*/
function expired($name)
{
if (!isset($this->info[$name]))
{
return true;
}
else
{
return ($this->info[$name]<time());
}
}
/**
* Clears all the cache files
*/
function clear()
{
$this->_write_info();
foreach (array_keys($this->info) as $name)
{
@unlink(CACHE . CACHE_COMPONENT_INFO_DIR . $name);
$this->deleted[]=$name;
}
$this->_write_info();
}
}
?>
New cache system March 14, 2006
Posted by rossoft in CakePHP.1 comment so far
In nightly version of cake, there is a new cache system. It can cache the views with cache helper, but you can cache your own data.
Example for caching your own data. You have the array $data. That array comes from the DB querying the model MyModel with parameter $id
$cacheName = “my_controller/$id”;
$data=cache($cacheName,null,’+1 hour’); //check for the value in the cache with a maximum of 1 hour old
if (!$data) //if isn’t on cache…
{
$this->MyModel->id=$id;
$data=$this->MyModel->read();
cache($cacheName,$data); //save to cache
}
You have to create the dir /app/tmp/cache/my_controller (set the correct permissions)
New search to ALL cakephp sites at once March 13, 2006
Posted by rossoft in CakePHP.2 comments
Now you can search on the API, Trac, CakeForge, Manual, Google Group, and several blogs (mine included :))
The search engine is from the url
http://cakephp.org/search/
Search the cakephp site within firefox search bar March 9, 2006
Posted by rossoft in CakePHP.1 comment so far
The source code of the plugin is:
cakephp_api.src
# Search CakePHP Api
# By Rossoft rossoft ___@___ gmail.com
<search
version=”7.1″
name=”CakePHP Api”
description=”Search the API of CakePHP”
sourceTextEncoding=”0″
method=”GET”
action=”http://api.cakephp.org/search.php”
queryCharset=”UTF-8″
searchForm=”http://api.cakephp.org/”
>
<input name=”query” user>
</search>
<browser
update=”http://mycroft.mozdev.org/update.php/id0/cakephp_api.src”
updateicon=”http://mycroft.mozdev.org/update.php/id0/cakephp_api.png”
updateCheckDays=”7″
>
cakephp_wiki.src
# Search CakePHP Wiki
# By Rossoft rossoft ___@___ gmail.com
<search
version=”7.1″
name=”CakePHP Wiki”
description=”Search the Wiki of CakePHP”
sourceTextEncoding=”0″
method=”GET”
action=”http://wiki.cakephp.org/”
queryCharset=”UTF-8″
searchForm=”http://wiki.cakephp.org/”
>
<input name=”id” user>
<input name=”do” value=”search”>
</search>
<browser
update=”http://mycroft.mozdev.org/update.php/id0/cakephp_wiki.src”
updateicon=”http://mycroft.mozdev.org/update.php/id0/cakephp_wiki.png”
updateCheckDays=”7″
>