-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapchat_cache.php
More file actions
59 lines (51 loc) · 1.14 KB
/
snapchat_cache.php
File metadata and controls
59 lines (51 loc) · 1.14 KB
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
<?php
/**
* @file
* Provides a storage class for the high-level Snapchat object. Caching
* results prevents unnecessary requests to the API.
*/
class SnapchatCache {
/**
* The lifespan of the data in seconds. This might be able to be customized
* at some point in the future.
*/
private static $_lifespan = 2;
/**
* The cache data itself.
*/
private $_cache = array();
/**
* Gets a result from the cache if it's fresh enough.
*
* @param string $key
* The key of the result to retrieve.
*
* @return mixed
* The result or FALSE on failure.
*/
public function get($key) {
// First, check to see if the result has been cached.
if (!isset($this->_cache[$key])) {
return FALSE;
}
// Second, check its freshness.
if ($this->_cache[$key]['time'] < time() - self::$_lifespan) {
return FALSE;
}
return $this->_cache[$key]['data'];
}
/**
* Adds a result to the cache.
*
* @param string $key
* The key of the result to store.
* @param mixed $data
* The data to store.
*/
public function set($key, $data) {
$this->_cache[$key] = array(
'time' => time(),
'data' => $data,
);
}
}