Skip to content

Commit a1cfdcd

Browse files
authored
Merge pull request #77 from codebard/master
Add access token to cache hash logic. Make cache var and function non static.
2 parents 4608843 + a06505e commit a1cfdcd

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 1.0.2
2+
3+
* Made caching function and variable static to accommodate sites/apps that use more than one client or do backend processing of
4+
multiple different patrons using saved tokens. If you were directly calling cache function or variable in your app, you will need to change those calls from static to non static
5+
* Added access token to hash mechanism that creates hashes for request cache
6+
* Added args to get_data function that calls the API
7+
* Implemented a 'skip_read_from_cache' var to skip reading a request from cache in case it exists in the cache
8+
19
# 1.0.0
210

311
* Library moved to use Patreon API v2 endpoints and calls

src/API.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class API {
1010
public $api_endpoint;
1111

1212
// The cache for request results - an array that matches md5 of the unique API request to the returned result
13-
public static $request_cache;
13+
public $request_cache;
1414

1515
// Sets the reqeuest method for cURL
1616
public $api_request_method = 'GET';
@@ -73,19 +73,21 @@ public function fetch_page_of_members_from_campaign($campaign_id, $page_size, $c
7373

7474
}
7575

76-
public function get_data($suffix) {
77-
76+
public function get_data( $suffix, $args = array() ) {
77+
7878
// Construct request:
7979
$api_request = $this->api_endpoint . $suffix;
8080

8181
// This identifies a unique request
82-
$api_request_hash = md5($api_request);
82+
$api_request_hash = md5( $this->access_token . $api_request );
8383

8484
// Check if this request exists in the cache and if so, return it directly - avoids repeated requests to API in the same page run for same request string
8585

86-
if ( isset( self::$request_cache[$api_request_hash] ) ) {
87-
return self::$request_cache[$api_request_hash];
88-
}
86+
if ( !isset( $args['skip_read_from_cache'] ) ) {
87+
if ( isset( $this->request_cache[$api_request_hash] ) ) {
88+
return $this->request_cache[$api_request_hash];
89+
}
90+
}
8991

9092
// Request is new - actually perform the request
9193

@@ -96,12 +98,12 @@ public function get_data($suffix) {
9698

9799
// don't try to parse a 500-class error, as it's likely not JSON
98100
if ( $info['http_code'] >= 500 ) {
99-
return self::add_to_request_cache($api_request_hash, $json_string);
101+
return $this->add_to_request_cache($api_request_hash, $json_string);
100102
}
101103

102104
// don't try to parse a 400-class error, as it's likely not JSON
103105
if ( $info['http_code'] >= 400 ) {
104-
return self::add_to_request_cache($api_request_hash, $json_string);
106+
return $this->add_to_request_cache($api_request_hash, $json_string);
105107
}
106108

107109
// Parse the return according to the format set by api_return_format variable
@@ -119,7 +121,7 @@ public function get_data($suffix) {
119121
}
120122

121123
// Add this new request to the request cache and return it
122-
return self::add_to_request_cache($api_request_hash, $return);
124+
return $this->add_to_request_cache($api_request_hash, $return);
123125

124126
}
125127

@@ -144,29 +146,29 @@ private function __create_ch($api_request) {
144146

145147
$headers = array(
146148
'Authorization: Bearer ' . $this->access_token,
147-
'User-Agent: Patreon-PHP, version 1.0.0, platform ' . php_uname('s') . '-' . php_uname( 'r' ),
149+
'User-Agent: Patreon-PHP, version 1.0.2, platform ' . php_uname('s') . '-' . php_uname( 'r' ),
148150
);
149151

150152
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
151153
return $ch;
152154

153155
}
154156

155-
public static function add_to_request_cache( $api_request_hash, $result ) {
157+
public function add_to_request_cache( $api_request_hash, $result ) {
156158

157159
// This function manages the array that is used as the cache for API requests. What it does is to accept a md5 hash of entire query string (GET, with url, endpoint and options and all) and then add it to the request cache array
158160

159161
// If the cache array is larger than 50, snip the first item. This may be increased in future
160162

161-
if ( !empty(self::$request_cache) && (count( self::$request_cache ) > 50) ) {
162-
array_shift( self::$request_cache );
163+
if ( !empty($this->request_cache) && (count( $this->request_cache ) > 50) ) {
164+
array_shift( $this->request_cache );
163165
}
164166

165167
// Add the new request and return it
166168

167-
return self::$request_cache[$api_request_hash] = $result;
169+
return $this->request_cache[$api_request_hash] = $result;
168170

169171
}
170172

171173

172-
}
174+
}

src/OAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private function __update_token($params) {
3535
curl_setopt($ch, CURLOPT_URL, $api_endpoint);
3636
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
3737
curl_setopt($ch, CURLOPT_POST, 1);
38-
curl_setopt($ch, CURLOPT_USERAGENT, "Patreon-PHP, version 1.0.0, platform ".php_uname('s').'-'.php_uname('r'));
38+
curl_setopt($ch, CURLOPT_USERAGENT, "Patreon-PHP, version 1.0.2, platform ".php_uname('s').'-'.php_uname('r'));
3939
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
4040
return json_decode(curl_exec($ch), true);
4141
}

0 commit comments

Comments
 (0)