-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache.php
More file actions
49 lines (45 loc) · 1.56 KB
/
Copy pathcache.php
File metadata and controls
49 lines (45 loc) · 1.56 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
<?php
class uCache {
/**
* Cached files are stale if older than four weeks by default
*/
public static $staleTime = 2419200;
/**
* Get path to cache file if file exists and is not stale
*
* @param $identifiers Unique information about this data, used to generate hash
* @param $staleTime NULL to use default, FALSE to ignore file age, integer to specify maximum age in seconds
* @return False if cache not found, otherwise returns absolute path to cache file
*/
public static function retrieve($identifiers,$staleTime = null) {
$path = self::getPath($identifiers);
if (!file_exists($path)) return false;
if ($staleTime === null) $staleTime = self::$staleTime;
if ($staleTime !== false && (filemtime($path) < (time()-$staleTime))) return false;
return $path;
}
/**
* Saves data in a cache file and returns the path
*
* @param $identifiers Unique information about this data, used to generate hash
* @return absolute path to cache file
*/
public static function store($identifiers,$data) {
$path = self::getPath($identifiers);
file_put_contents($path,$data);
return $path;
}
/**
* Get path to cache file
*
* @param $identifiers Unique information about this data, used to generate hash
* @return absolute path to cache file
*/
private static function getPath($identifiers) {
$cachePath = PATH_ABS_CORE.'.cache/';
$checksum = utopia::checksum($identifiers);
$cachePath .= substr($checksum,0,3).'/'.substr($checksum,3,3).'/';
if (!file_exists($cachePath)) mkdir($cachePath,0777,true);
return $cachePath.$checksum;
}
}