简体   繁体   English

菲尔·斯特金的缓存库

[英]Phil Sturgeon's Cache Library

Ok here is the link to the cache library he wrote: https://github.com/philsturgeon/codeigniter-cache 好的,这是他写的缓存库的链接: 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? 如果是的话,$ data到底应该持有什么值? The overall query orrr...?? 整体查询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. 这将在模型blog_m上调用方法getPosts ,并将结果缓存120秒。 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. 如果在接下来的2分钟内再次进行相同的调用,它将返回缓存的结果,否则它将从数据库中获取数据并更新缓存。 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. $data将保存您要缓存的任何内容。 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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