Completed
Push — master ( b88251...ac7e0c )
by Loban
03:36
created

LangCache::invalidateCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace lav45\translate\models;
4
5
use yii\di\Instance;
6
use yii\caching\CacheInterface;
7
use yii\caching\TagDependency;
8
9
/**
10
 * Class LangCache
11
 * @package lav45\translate\models
12
 */
13
class LangCache extends Lang
14
{
15
    /**
16
     * @var string
17
     */
18
    public static $cacheKey = 'LangCache::cacheKey';
19
    /**
20
     * @var string|array|CacheInterface
21
     */
22
    public $cache = 'cache';
23
24
    /**
25
     * @return \yii\caching\CacheInterface
26
     */
27
    public function getCache()
28
    {
29
        return Instance::ensure($this->cache, 'yii\caching\CacheInterface');
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function afterDelete()
36
    {
37
        parent::afterDelete();
38
        $this->invalidateCache();
39
    }
40
41
    /**
42
     * @inheritdoc
43
     * @param bool $insert
44
     * @param array $changedAttributes
45
     */
46
    public function afterSave($insert, $changedAttributes)
47
    {
48
        parent::afterSave($insert, $changedAttributes);
49
        if (!empty($changedAttributes)) {
50
            $this->invalidateCache();
51
        }
52
    }
53
54
    /**
55
     * Invalidate cache for all cached lists
56
     */
57
    public function invalidateCache()
58
    {
59
        TagDependency::invalidate($this->getCache(), static::$cacheKey);
60
    }
61
62
    /**
63
     * @return TagDependency
64
     */
65
    public static function getDependency()
66
    {
67
        return new TagDependency(['tags' => static::$cacheKey]);
68
    }
69
70
    /**
71
     * @param bool $active default false
72
     * @return array
73
     */
74
    public static function getList($active = false)
75
    {
76
        return static::getDb()->cache(function() use ($active) {
77
            return parent::getList($active);
78
        }, null, static::getDependency());
79
    }
80
81
    /**
82
     * @param bool $active default true
83
     * @return array
84
     */
85
    public static function getLocaleList($active = true)
86
    {
87
        return static::getDb()->cache(function() use ($active) {
88
            return parent::getLocaleList($active);
89
        }, null, static::getDependency());
90
    }
91
}