简体   繁体   中英

Phil Sturgeon's Cache Library

Ok here is the link to the cache library he wrote: https://github.com/philsturgeon/codeigniter-cache

Anyway, his documentation is absolutely.. vague and not helpful at all. I know it's self explanatory.. to a point.

$this->cache->model('blog_m', 'getPosts', array($category_id, 'live'), 120); // keep for 2 minutes 

What is the 3rd parameter? And is that what creates the cache or this what creates a cache file:

$this->cache->write($data, 'cached-name');

And if that is, what exactly is $data suppose to be holding a value of? The overall query orrr...?? If anyone could give explanation on this on how you create a cache file.. Basically I want to cache the query that selects a bunch of news postings.. and everytime a new new post is created, to delete that cache and recache it so it shows the new news posting..

The documentation seems to be fairly clear. Anyways, I'll try to explain it in better terms:

// cached model call
$this->cache->model('blog_m', 'getPosts', array($category_id, 'live'), 120); // keep for 2 minutes

This calls the method getPosts on the model blog_m and caches the result for 120 seconds. If you make the same call again within the next 2min, it will return the cached results, otherwise it will fetch the data from the database and update the cache. It's good for methods on models that you will be calling very frequently.

If you want to manually add and get data from a cache, then you use:

// cached array or object
$this->cache->write($data, 'cached-name');
$data = $this->cache->get('cached-name');

$data will hold whatever you want to cache. If you want to cache the user's email, for example, here's how you would cache and fetch it

$email = 'foobar@example.com';
$this->cache->write($email, 'user-email');
// to fetch
$user_email = $this->cache->get('user-email');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM