Surebert wikisurebert home | autogenerated toolkit docs | toolkit examples | Log in

Framework:sb Cache

From Surebert wiki

Contents

Overview

Caching data serverside is an essential part of tweaking a web application's performance. As applications scale and also begin to offer more and more dynamic data, it is important to be able to cache that data when it is not changes. A classic exmaple would be the list of latest blog entries on a blogging site. The list may join information from several different tables and be resource intensive. There is no need to request the data from the database on every call and then to transofrm the rows returned into html. Instead, you can have you model or view, load the data and then cache the resulting HTML into your application's cache. The next time it is loaded, it is instead loaded from the cache. Anytime, someone updates the list, you have you application destroy the cache.

Another example would be loading the current weather from the federal gov't site using sb_Weather. It is expensive to load the data dysmaically on every page request. You might think, why not just load it once and save it in a session. A session, unlike the application's cache is only available to the the same user and if all users are from the same place, the weather is not going to change on per visitor basis. It should be cached on a timed basis.

Basically, think any time you do anything that involves a lot a calculation or conenctions (CPU, bandwidth, or memory) it is best to store the calculated result for resuse, if the result does not change very often.

Surebert's caching engine allows you to store cached data in the file system, in a mysql database, on a memcache server or using APC. Depending on you needs and if you expect the application to have to scale across servers, you can choose the format that is best for you.

sb_Cache

Instantiating a cache is easy to do and should be done in your applications definitions.php file. You can have one single cache for your app, or multiple caches of different types. For example, you could connect to three different memcache servers with different data.

Type of sb_Cache

Current there are four types of sb_Cache but each one works the same.

FileSystem

Store the data in a structured directory inside of /private/cache in your web application. This is an extremely fast caching system, but is only available on your server. If you are creating a web app which must scale horizontally across servers this would not be your best option. Otherwise, this is ideal.

php code

$cache = new sb_Cache_FileSystem();

Mysql

Stores the cached data in a mysql table. It cannot use a memory table because in memory tables do not support TEXT columns. If you wish to store large amounts of cache data in memory and across servers use sb_Cache_Memcache(). One of the main advantages of mysql is that you can scale by sharing the cache between instances of the web application.

php code

$cache = new sb_Cache_Mysql($pdo_conn);

Memcache

Uses the network/memory based memcahed daemon to store data. This is also great for large sites that need to scale, or when you want to share the cache between web applications. The memcached server can be on localhost, or across the web.

php code

$cache = new sb_Cache_Memcache($host, $port, $namespace);

APC

Uses PHP alternative Cache which is currently a PECL module but will be built into PHP 6. This type of caching is extremly fast and stores the data in memory on the local machine.

php code

$cache = new sb_Cache_APC($namespace);

sb_Cache Methods

Cache keys are always written in filesystem / format, even if you are using sb_Cache_Mysql or sb_Cache_Memcache or sb_Cache_APC

$cache->store($key, $value, $lifetime_in_seconds);

If you do not define $lifetime_in_seconds or set it to 0, the cache is non-expiring - meaning that you must delete it manually to remove it. This is good in the case of data that only changes after an update. E.g. an RSS feed - there is no need to delete the cache until there is a new news story.

php code

$cache->store('/paul/blog/date_list', $html, 300);

$cache->fetch($key);

Fetch data back out from the cache

php code

$cache->fetch('/paul/blog/date_list');

$cache->delete($key);

Deletes data out of the cache. You can delete the full path or any parent path of the cached data to recurively delete cached information

php code

//deletes just date_list
$cache->fetch('/paul/blog/date_list');

php code

//deletes everything in paul/blog
$cache->fetch('/paul/blog/');

php code

//deletes everything in paul
$cache->fetch('/paul/');

$cache->get_keys();

This returns an array with all of the currently stored cache keys in the cache as the key and their time left in seconds as the value. This was previously $cache->catalog_load() but was renamed after release 1.0

php code

$keys = $cache->get_keys();
print_r($keys);

$cache->clear_all();

Deleted all data in the cache

php code

$catalog = $cache->clear_all();

Retrieved from "http://wiki.surebert.com/index.php/Framework:sb_Cache"

This page has been accessed 496 times. This page was last modified on 25 February 2009, at 16:53. Content is available under Attribution-Noncommercial-Share Alike 3.0 Unported.


Browse
Home
Toolkit
Framework
Application
Recent changes
Random page
Edit
View source
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Log in / create account
Special pages
New pages
File list
Statistics
More...