-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathAbstractCachingService.php
90 lines (75 loc) · 1.76 KB
/
AbstractCachingService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Created by PhpStorm.
* User: andy
* Date: 02.08.15
* Time: 23:33
*/
namespace JsLocalization\Caching;
use Cache;
use DateTime;
abstract class AbstractCachingService {
/**
* The key used to cache the data.
*
* @var string
*/
protected $cacheKey;
/**
* The key used to cache the timestamp of the last
* refreshCache() call.
*
* @var string
*/
protected $cacheTimestampKey;
/**
* @param string $cacheKey
* @param string $cacheTimestampKey
*/
public function __construct($cacheKey, $cacheTimestampKey)
{
$this->cacheKey = $cacheKey;
$this->cacheTimestampKey = $cacheTimestampKey;
}
/**
* Returns the timestamp of the last refreshCache() call.
*
* @return DateTime
*/
public function getLastRefreshTimestamp()
{
$unixTime = Cache::get($this->cacheTimestampKey);
$dateTime = new DateTime;
$dateTime->setTimestamp($unixTime);
return $dateTime;
}
/**
* Refresh the cached data.
*
* @param mixed $data
*/
public function refreshCacheUsing($data)
{
Cache::forever($this->cacheKey, $data);
Cache::forever($this->cacheTimestampKey, time());
}
/**
* Create up-to-date data and update cache using refreshCacheUsing().
* Trigger some event.
*
* @return void
*/
abstract public function refreshCache();
/**
* Return the cached data. Refresh cache if cache has not yet been initialized.
*
* @return mixed
*/
public function getData()
{
if (!Cache::has($this->cacheKey)) {
$this->refreshCache();
}
return Cache::get($this->cacheKey);
}
}