Completed
Push — master ( 1459ee...4560a8 )
by David
03:37
created

CacheTrait::deletingEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 1
eloc 4
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A CacheTrait::removeFromCache() 0 4 1
1
<?php namespace App\LaravelRestCms;
2
3
trait CacheTrait {
4
5
    /**
6
     * The key to use to cache this model
7
     * @var string
8
     */
9
    public static $cacheKeyPart = 'id';
10
    
11
    /**
12
     * The
13
     * @var [type]
14
     */
15
    public static $cacheTime = '3600'; 
16
        
17
    
18
    /**
19
     * Tie caching features into the Eloquent::boot() method
20
     */
21
    public static function bootCacheTrait()
22
    {
23
        static::$cacheTime = \Config::get('laravel-rest-cms.cacheTime');
24
    }
25
26
    /**
27
     * Adds a model to the cache
28
     * 
29
     * @param  string   $model      The name of the model
30
     * @return void
31
     */
32
    protected static function addToCache($model)
33
    {
34
        $model::cache($model, $model->{$model::$cacheKeyPart}, $model::getModelCache($model));
35
    }
36
37
    /**
38
     * Removes a model from the cache
39
     * 
40
     * @param  string   $model      The name of the model
41
     * @return void
42
     */
43
    protected static function removeFromCache($model)
44
    {
45
        \Cache::forget($model::getCacheKey($model));
46
    }
47
48
    /**
49
     * Retrieves the model
50
     * Override this function to create a custom cache with eager loading
51
     * 
52
     * @param  string $model The name of the model
53
     * @return string
54
     */
55
    public static function getModelCache($model)
56
    {
57
        return $model;
58
    }       
59
    
60
    /**
61
     * Caches a model
62
     * 
63
     * @param  string   $model      The name of the model
64
     * @param  string   $keyPart    The key to use to cache the model (e.g. primary key)
65
     * @param  App\LaravelRestCms\BaseModel $data   The model instance
66
     * @return string
67
     */
68
    public static function cache($model, $keyPart, $data)
69
    {
70
        $key = static::getCacheKey($model, $keyPart);
71
        \Cache::forget($key);
72
        \Cache::put($key, $data, static::$cacheTime);
73
        
74
        return $model;
75
    }       
76
    
77
    /**
78
     * Caches a model
79
     * 
80
     * @param  string   $model      The name of the model
81
     * @param  string   $keyPart    The key to use to cache the model (e.g. primary key)
82
     * @return string
83
     */
84
    public static function getCache($model, $keyPart)
85
    {
86
        $key = static::getCacheKey($model, $keyPart);
87
        
88
        return \Cache::get($key);
89
    }
90
    
91
    /**
92
     * Gets the cache key for a model
93
     * 
94
     * @param  string   $model      The name of the model
95
     * @param  string   $keyPart    The key to use to cache the model (e.g. primary key)
96
     * @return string
97
     */
98
    public static function getCacheKey($model, $keyPart = null)
99
    {
100
        if (is_null($keyPart)) {
101
            $keyPart = static::$cacheKeyPart;
102
        }
103
        
104
        return with(new $model)->getTable() . '.' . $keyPart;
105
    }
106
}