-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempCache.php
executable file
·155 lines (143 loc) · 2.69 KB
/
TempCache.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Cajogos;
/**
* Class TempCache
* @package Cajogos
*/
class TempCache
{
const TIME_5_MIN = 300;
const TIME_HALF_HOUR = 1800;
const TIME_1_HOUR = 3600;
const TIME_12_HOUR = 43200;
const TIME_1_DAY = 86400;
const TIME_1_WEEK = 604800;
const CACHE_FOLDER = '/tmp/php-temp-cache/';
/**
* @var bool
*/
private static $prepared = false;
/**
* @param string $key
* @return mixed|null
*/
public static function get($key)
{
if (self::prepare())
{
$key = self::cache_key_convert($key);
$file = $key . '.cache';
$contents = self::load_cache_file($file);
if ($contents === false)
{
return null;
}
$json_content = json_decode($contents);
$expiry = $json_content->expiry;
if (time() > $expiry)
{
self::delete_cache_file($file);
return null;
}
$value = unserialize($json_content->value);
return $value;
}
return null;
}
/**
* @param string $key
* @param string $value
* @param integer $expiry
* @return bool
*/
public static function put($key, $value, $expiry)
{
if (self::prepare())
{
$cache_key = $key;
$key = self::cache_key_convert($key);
$file = $key . '.cache';
$expiry = time() + $expiry;
$serialized = serialize($value);
$store = array(
'key' => $cache_key,
'expiry' => $expiry,
'value' => $serialized
);
$store = json_encode($store);
return self::save_cache_file($file, $store);
}
return false;
}
/**
* @param string $key
* @return bool
*/
public static function remove($key)
{
if (self::prepare())
{
$key = self::cache_key_convert($key);
$file = $key . '.cache';
return self::delete_cache_file($file);
}
return false;
}
/**
* @param string $file
* @return bool|string
*/
private static function load_cache_file($file)
{
$file = self::CACHE_FOLDER . $file;
if (file_exists($file))
{
return file_get_contents($file);
}
return false;
}
/**
* @param string $file
* @param string $data
* @return bool
*/
private static function save_cache_file($file, $data)
{
$file = self::CACHE_FOLDER . $file;
return (!(file_put_contents($file, $data) === false));
}
/**
* @param string $file
* @return bool
*/
private static function delete_cache_file($file)
{
$file = self::CACHE_FOLDER . $file;
return unlink($file);
}
/**
* @param string $key
* @return string
*/
private static function cache_key_convert($key)
{
$key = 'tmpcache_' . $key;
return md5($key);
}
/**
* @return bool
*/
private static function prepare()
{
if (self::$prepared)
{
return true;
}
self::$prepared = true;
if (file_exists(self::CACHE_FOLDER) === false)
{
return mkdir(self::CACHE_FOLDER);
}
return true;
}
}