forked from IBM-Cloud/get-started-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSagCache.php
More file actions
162 lines (144 loc) · 4.49 KB
/
SagCache.php
File metadata and controls
162 lines (144 loc) · 4.49 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
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
156
157
158
159
160
161
162
<?php
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require_once("SagException.php");
/**
* All the caching systems that Sag can leverage must extend this. The cache
* values should always be the object that Sag::procPacket() would return.
*
* The default cache size is 1MB (one million bytes).
*
* Cache values are objects (stdClass for PHP storage, or JSON for external
* storage).
*
* @package Cache
* @version 0.9.0
*/
abstract class SagCache {
private $maxSize; //in bytes
private $currentSize; //in bytes
public function __construct() {
$this->currentSize = 0;
$this->maxSize = 1000000;
}
/**
* Returns the cached object or null if nothing is cached.
*
* @param string $url The URL of the request we're caching.
* @return object
*/
abstract public function get($url);
/**
* Caches the item returned at the provided key, replacing any pre-existing
* cached item. If the cache's size will be exceeded by caching the new item,
* then it will remove items from the cache until there is sufficient room.
*
* Returns false if the item cannot be added to the cache for any reason:
* exceeds the cache size, invalid type, or relevant HTTP headers.
*
* Returns true if we were able to add the item, and there was no previously
* cached item.
*
* Returns the previously cached item if there was one and we were able to
* add the new item to the cache.
*
* Sag will refuse to cache the object by throwing a SagException if adding
* the file to the cache would exceed 95% of the disk or partition's free
* space.
*
* @param string $url The URL of the request we're caching.
* @param object $item The response we're caching.
* @return mixed
*/
abstract public function set($url, &$item);
/**
* Removes the item from the cache and returns it (null if nothing was
* cached).
*
* @param string $url The URL of the response we're removing from the cache.
* @return mixed
*/
abstract public function remove($url);
/**
* Clears the whole cache without applying any logic.
*
* Returns true if the entire cache was cleared, otherwise false if only part
* or none of it was cleared.
*
* @return bool
*/
abstract public function clear();
/**
* Sets the max size of the cache in bytes.
*
* @param int $bytes The size of the cache in bytes (>0).
*/
public function setSize($bytes) {
if(!is_int($bytes) || $bytes <= 0) {
throw new Exception("The cache size must be a positive integer (bytes).");
}
$this->maxSize = $bytes;
}
/**
* Returns the max size of the cache, irrespective of what is or isn't in the
* cache.
*
* @return int
*/
public function getSize() {
return $this->maxSize;
}
/**
* Returns the total size of the items in the cache in bytes. Not reliable if
* you're using SagMemoryCache.
*
* @return int
*/
public function getUsage() {
return $this->currentSize;
}
/**
* Generates the hash of the provided URL that will be used as the cache key.
*
* @param string $url The URL to hash.
* @return string
*/
public function makeKey($url) {
return sha1($url);
}
protected function addToSize($amt) {
if(!is_int($amt) && !is_float($amt)) {
throw new SagException('Invalid cache size modifier.');
}
$this->currentSize += $amt;
}
/**
* Returns whether or not the item may be cached. It has to be a stdClass
* that Sag would return, with a valid E-Tag, and no cache headers that tell
* us to not cache.
*
* @param The item that we're trying to cache - it should be a response as a
* stdClass.
* @return bool
*/
protected function mayCache($item) {
return (
isset($item) &&
is_object($item) &&
isset($item->headers) &&
is_string($item->headers->etag) &&
!empty($item->headers->etag) &&
isset($item->body) &&
is_object($item->body)
);
}
}