CacheTrait::addToCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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