intersvyaz/yii-tags-dependency

基于Yii框架依赖机制和存储在缓存中的标签验证缓存相关性

1.0.1 2014-12-09 17:06 UTC

This package is auto-updated.

Last update: 2024-09-08 07:17:00 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

基于Yii框架依赖机制和存储在缓存中的标签验证缓存相关性

基于科济金·亚历山大 < http://habrahabr.ru/users/kosalnik/ > 的想法,他在 http://habrahabr.ru/post/159079/ 描述了该想法

通过Composer安装

php composer.phar require intersvyaz/yii-tags-dependency:*

配置

  1. 此扩展需要配置缓存

基本用法

<?php

use Intersvyaz\Cache\TagsDependency;

$cache = \Yii::app()->cache;

// save any value into cache with this dependency
$cache->set('cacheKey', 'cacheValue', 0, new TagsDependency(['A', 'B']));

// check if there is a value in cache
var_dump($cache->get('cacheKey'));

// remove (invalidate) one or several tags
TagsDependency::clearTags(['A']);

// check if cached value is absent in cache
var_dump($cache->get('cacheKey'));

CacheTagBehavior使用

class TestActiveRecord extends \CActiveRecord
{
//    ...
    
    public function behaviors()
    {
        return [
            'cacheTagBehavior' => [
                'class' => CacheTagBehavior::class,
            ]
        ];
    }
    
//    ...
}

// in other code: 

$models = TestActiveRecord::model()->cacheTag(3600, $dependency)->findAll();

// ...

// read query from cache
$models = TestActiveRecord::model()->cacheTag(3600, $dependency)->findAll();

// ...
$model2 = TestActiveRecord::model()->findByPk(2);
$model2->title = 'test';
$model2->save();

// ...

// cache invalid, read query from db
$models = TestActiveRecord::model()->cacheTag(3600, $dependency)->findAll();