Quantcast
Channel: PHP development, Web development, Mobile Appication, Groupon clone script » cache
Viewing all articles
Browse latest Browse all 2

Data Cache enabled by Kohana PHP

$
0
0

Data Cache enabled by Kohana PHP

Kohana php lets you cache any data in order to achieve maximum performance. The majority of web pages served today are generated dynamically, usually by an application server querying a back-end database. Caching consists in storing objects or pages in their fully rendered state so that they are directly loaded for next requests. It allows to cut response times and save server resources and memory.

Any objects or content requiring “heavy” dynamic generation. Kohana’s cache library can currently store caches in various containers including file and database. This is easily configurable by setting.

the driver. Cached objects or contents can be loaded using a powerful tag system or with their identifier.

Kohana php can store the locations of all auto loaded classes in a file (enable ‘internal config’ in your config.php)
Configuration

Configuration is done in the application/config/cache.php file, if it’s not there take the one from system/config and copy it to the application folder (see cascading filesystem):

$config['default'] = array(
‘driver’ => ‘file’,
‘params’ => APPPATH.’cache’,
‘lifetime’ => 1800,
‘requests’ => 1000
);

Drivers

config['driver'] sets the driver, which is the container for your cached files. There are 6 different drivers:

File – File cache is fast and reliable, but requires many file system lookups.
SQlite – Database cache can be used to cache items remotely, but is slower.
Memcache – Memcache is very high performance, but prevents cache tags from being used.
APC – Alternative Php Cache
Eaccelerator
Xcache

Driver parameters

$config['params'] contains driver specific parameters. (in above example – path to server writable cache dir)

Cache Lifetime

$config['lifetime'] sets the lifetime of the cache (in seconds). Specific lifetime can be set when creating a new cache. 0 means it will never be deleted automatically.You can set the cache time to suit the application.

Suppose you want to get some information from your database and then want to build a table of the entries you get. To cache the generated content, you would use in your controller code like this:

$this->cache= Cache::instance();

$table = $this->cache->get(‘table’);

if ( ! $table) {
$table = build_table();
$this->cache->set(‘table’, $table, array(‘mytag1′, ‘mytag2′), 3600);
}

echo $table;

There are 3 main steps:
Instantiate the cache library
Get the cache:
If the cache doesn’t exist, we can build the table by querying the database, we cache it for 1 hour (3600 seconds) for next requests and we print it.
If the cache exists, we directly print the cached version of the table

Loading the library

$this->cache = Cache::instance();

Methods

$this→cache→set($id, $data, $tags = NULL, $lifetime = NULL) is used to set caches.

$this→cache→get($id) retrieves a cache with the given $id, returns the data or NULL

$this→cache→find($tag) supply with a string, retrieves all caches with the given tag.

$this→cache→delete($id) deletes a cache item by id, returns a boolean.

$this→cache→delete_tag($tag) deletes all cache items with a given tag, returns a boolean.

$this→cache→delete_all() deletes all cache items, returns a boolean.

First is cache time settings, second one is getting cache for a given ID (it accepts the numeric id and returns the data or NULL), third one is supply with a string  to get all caches with the given tag,
fourth one is caching an item by id returning a boolean operator ‘and, ‘or’  deletes all cache items witha given tag(negative key words) and return a boolean operator, deletes all cache items and

returns a boolean operator. This method is able to retrieve the exact page to be cached by the search engines.

Setting caches
set

$this→cache→set($id,$data,$tags = NULL, $lifetime = NULL) is used to set caches.

*
$id The id should be unique
$data If $data is not a string it will be serialized for storage.
$tagsdefaults to none, an array should be supplied. This is useful when grouping caches together.

$lifetime specific lifetime can be set. If none given the default lifetime from the configuration file will be used.

$data=array(‘ atomic number 6 ‘, ‘allotropes ‘, ‘nonmetallic’);

$tags=array(‘carbon’,'tetravalent’,'element’);
$this->cache->set(‘tetravalent’,$data,$tags);

Finding and getting caches
get

$this→cache→get($id) retrieves a cache with the given $id, returns the data or NULL

print_r($this->cache->get(‘tetravalent’));
//returns:
// Array ( [0] => atomic number 6 [1] => allotropes [2] =>nonmetallic )

find

$this→cache→find($tag) supply with a string, retrieves all caches with the given tag.

$element=array(‘element valency’,’ element property’,’ element luster ‘);

$this->cache->set(‘element’,$element,array(‘element’));

print_r($this->cache->find(‘metallic’));
//returns
//Array ( [tetravalent] => Array ( [0] => carbon [1] => Atomic number 6 [2] => nonmetallic ) [element] => Array ( [0] =>element valency  [1] =>  [2] element property=>element luster))

Let us see more about caching in the forthcoming blogs.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images