forked from BrightcoveOS/PHP-MAPI-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bc-mapi-cache.php
143 lines (129 loc) · 4.23 KB
/
bc-mapi-cache.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
<?php
/**
* Brightcove PHP MAPI Wrapper Caching Layer 1.0.0 (12 OCTOBER 2010)
*
* REFERENCES:
* Website: http://opensource.brightcove.com
* Source: http://github.com/brightcoveos
*
* AUTHORS:
* Matthew Congrove <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the �Software�),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, alter, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following conditions:
*
* 1. The permission granted herein does not extend to commercial use of
* the Software by entities primarily engaged in providing online video and
* related services.
*
* 2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, SUITABILITY, TITLE,
* NONINFRINGEMENT, OR THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY WHATSOEVER, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
* THE SOFTWARE OR THE USE, INABILITY TO USE, OR OTHER DEALINGS IN THE SOFTWARE.
*
* 3. NONE OF THE AUTHORS, CONTRIBUTORS, NOR BRIGHTCOVE SHALL BE RESPONSIBLE
* IN ANY MANNER FOR USE OF THE SOFTWARE. THE SOFTWARE IS PROVIDED FOR YOUR
* CONVENIENCE AND ANY USE IS SOLELY AT YOUR OWN RISK. NO MAINTENANCE AND/OR
* SUPPORT OF ANY KIND IS PROVIDED FOR THE SOFTWARE.
*/
class BCMAPICache
{
public static $extension = NULL;
public static $location = NULL;
public static $memcached = NULL;
public static $port = NULL;
public static $time = 0;
public static $type = NULL;
/**
* The constructor for the BCMAPICache class.
* @access Public
* @since 1.0.0
* @param string [$type] The type of caching method to use, either 'file' or 'memcached'
* @param int [$time] How many seconds until cache files are considered cold
* @param string [$location] The absolute path of the cache directory (file) or host (memcached)
* @param string [$extension] The file extension for cache items (file only)
* @param int [$port] The port to use (Memcached only)
*/
public function __construct($type = 'file', $time = 600, $location, $extension = '.c', $port = 11211)
{
if(strtolower($type) == 'file')
{
$type = 'file';
} else if(strtolower($type) == 'memcache' || strtolower($type) == 'memcached') {
$type = 'memcached';
$memcached = new Memcached();
$memcached->addServer($location, $port);
self::$memcached = $memcached;
} else {
$type = FALSE;
}
self::$extension = $extension;
self::$location = $location;
self::$port = $port;
self::$time = $time;
self::$type = $type;
}
/**
* Retrieves any valid cached data.
* @access Public
* @since 1.0.1
* @param string [$key] The cache file key
* @return mixed The cached data if valid, otherwise FALSE
*/
public function get($key)
{
if(self::$type == 'file')
{
$file = self::$location . md5($key) . self::$extension;
if(file_exists($file) && is_readable($file))
{
if((time() - filemtime($file)) <= self::$time)
{
return file_get_contents($file);
}
}
return FALSE;
} else if(self::$type == 'memcached') {
$data = self::$memcached->get($key);
if(self::$memcached->getResultCode() == Memcached::RES_SUCCESS)
{
return $data;
}
} else {
return FALSE;
}
}
/**
* Creates a cache of data.
* @access Public
* @since 1.0.0
* @param string [$key] The cache file key
* @param mixed [$data] The data to cache
*/
public function set($key, $data)
{
if(self::$type == 'file')
{
$file = self::$location . md5($key) . self::$extension;
if(is_writable(self::$location))
{
$handle = fopen($file, 'w');
fwrite($handle, json_encode($data));
fclose($handle);
}
} else if(self::$type == 'memcached') {
self::$memcached->set($key, $data, time() + self::$time);
} else {
return FALSE;
}
}
}
?>