定制 mikehaertl/flushable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mikehaertl/flushable

Composer 安装命令:

composer require mikehaertl/flushable

包简介

Yii dependency that allows to flush records from the cache

README 文档

README

This dependency can be used to flush an item from the cache. It allows you to invalidate any cached item like a cached ActiveRecord, a DAO query result, a cached fragment or a cached page.

##Usage

Import the class file

Either add this line on top of every class file where you want to use it:

Yii::import('ext.flushable.FlushableDependency');

or add ext.flushable.FlushableDependency right to the import section of your main.php configuration file.

How to use the dependency

When you create the dependency you have to provide a unique $id wich identifies your cached content.

$dependency = new FlushableDependency('myKey');

Now you can use it for data, fragment or page caching.

If you want to cache some ActiveRecord or DAO result the $id is usually the primary key. In this case you should add the model name to avoid conflicts among different cached model classes:

// Cache a ActiveRecord
$dependency = new FlushableDependency($id,'Post');
$post = Post::model()->cache(3600,$dependency)->findByPk($id);

How to flush a content from cache

Whenever you want to invalidate the cached data you can now call:

FlushableDependency::flushItem('myKey');

If you used the dependency for data caching and added a model name you must provide the same name again:

FlushableDependency::flushItem($id,'Post');

You could put this into the afterSave() method of your active record (change $id to $this->id) or just call it manually whenever you update the record.

You can also add an $expire parameter to the flushItem() call. It will specify how long the information about the change should be kept in the cache. So obviously this should be more than the longest time which you cache your content. The default is 3600.

How it works

Whenever an item is cached with this dependency it scans the cache for another key. This key is unique per cached item and composed from the two parameters you pass along to the constructor. This other cache value basically indicates 'The item has expired!'. So if no such key is found, the item in the cache is still valid.

With a call to flushItem() you will create such an expiration entry in the cache for the cached item. So when the dependency is evaluated the next time it will find something now and the cached content will no longer be seen as valid.

Changelog

Release 1.1.2 (IMPORTANT: This update requires to flush your cache!)

  • Changed dependency keys to work flawlessly with file caches (Thanks yiqing-95).

Release 1.1.1

  • Changed dependency value from time() to microtime() to fix timing issues in some scenarios

Release 1.1.0

  • Refactored class interface

Release 1.0.1

  • Initial release

统计信息

  • 总下载量: 7.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-03-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固