|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The cache object which reads and writes the GitHub api data |
|
4
|
|
|
* @package WordPress_GitHub_Sync |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class WordPress_GitHub_Sync_Cache |
|
9
|
|
|
*/ |
|
10
|
|
|
class WordPress_GitHub_Sync_Cache { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Cached blobs. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $blobs = array(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Cached trees. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $trees = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Cached commits. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $commits = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Clean out previous version of API data. |
|
35
|
|
|
* |
|
36
|
|
|
* This old data failed to save frequently, as it was too much data to |
|
37
|
|
|
* hold in a single MySQL row. While the idea of maintaining this data |
|
38
|
|
|
* permanently in the database seemed ideal, given that the response for |
|
39
|
|
|
* a given sha of a given type will never change, it was too much information. |
|
40
|
|
|
* Transients are much more appropriate for this type of data, and even if we lose it, |
|
41
|
|
|
* it can still be refetched from the GitHub API. |
|
42
|
|
|
* |
|
43
|
|
|
* The structure of this object, including the name `open` for the singleton |
|
44
|
|
|
* method, is a holdover from this original implementation, where we would save |
|
45
|
|
|
* to the the database on the WordPress's shutdown hook with a `close` method. |
|
46
|
|
|
* All of this no longer exists, but we should be good WordPress citizens |
|
47
|
|
|
* and delete the data we left behind, since it was large and we're no longer |
|
48
|
|
|
* using it. |
|
49
|
|
|
*/ |
|
50
|
9 |
|
public function __construct() { |
|
51
|
|
|
// Clear out previously saved information. |
|
52
|
9 |
|
if ( get_option( '_wpghs_api_cache' ) ) { |
|
53
|
9 |
|
delete_option( '_wpghs_api_cache' ); |
|
54
|
9 |
|
} |
|
55
|
9 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Fetch commit from cache by sha. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $sha Commit sha to fetch from cache. |
|
61
|
|
|
* |
|
62
|
|
|
* @return false|WordPress_GitHub_Sync_Commit |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function fetch_commit( $sha ) { |
|
65
|
3 |
|
$commit = $this->get( 'commits', $sha ); |
|
66
|
|
|
|
|
67
|
3 |
|
if ( $commit instanceof WordPress_GitHub_Sync_Commit ) { |
|
68
|
2 |
|
return $commit; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Save commit to cache by sha. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $sha Commit sha to cache by. |
|
78
|
|
|
* @param WordPress_GitHub_Sync_Commit $commit Commit to cache. |
|
79
|
|
|
* |
|
80
|
|
|
* @return WordPress_GitHub_Sync_Commit |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function set_commit( $sha, WordPress_GitHub_Sync_Commit $commit ) { |
|
83
|
1 |
|
return $this->save( 'commits', $sha, $commit, 0 ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Fetch tree from cache by sha. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $sha Tree sha to fetch from cache. |
|
90
|
|
|
* |
|
91
|
|
|
* @return false|WordPress_GitHub_Sync_Tree |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function fetch_tree( $sha ) { |
|
94
|
3 |
|
$tree = $this->get( 'trees', $sha ); |
|
95
|
|
|
|
|
96
|
3 |
|
if ( $tree instanceof WordPress_GitHub_Sync_Tree ) { |
|
97
|
2 |
|
return $tree; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Save tree to cache by sha. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $sha Tree sha to cache by. |
|
108
|
|
|
* @param WordPress_GitHub_Sync_Tree $tree Tree to cache. |
|
109
|
|
|
* |
|
110
|
|
|
* @return WordPress_GitHub_Sync_Tree |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function set_tree( $sha, WordPress_GitHub_Sync_Tree $tree ) { |
|
113
|
1 |
|
return $this->save( 'trees', $sha, $tree, DAY_IN_SECONDS * 3 ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Fetch tree from cache by sha. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $sha Blob sha to fetch from cache. |
|
120
|
|
|
* |
|
121
|
|
|
* @return false|WordPress_GitHub_Sync_Blob |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function fetch_blob( $sha ) { |
|
124
|
3 |
|
$blob = $this->get( 'blobs', $sha ); |
|
125
|
|
|
|
|
126
|
3 |
|
if ( $blob instanceof WordPress_GitHub_Sync_Blob ) { |
|
127
|
2 |
|
return $blob; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Save blob to cache by sha. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $sha Blob sha to cache by. |
|
137
|
|
|
* @param WordPress_GitHub_Sync_Blob $blob Blob to cache. |
|
138
|
|
|
* |
|
139
|
|
|
* @return WordPress_GitHub_Sync_Blob |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function set_blob( $sha, WordPress_GitHub_Sync_Blob $blob ) { |
|
142
|
1 |
|
return $this->save( 'blobs', $sha, $blob, 3 * DAY_IN_SECONDS ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Retrieve data from previous api calls by sha. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $type Object type to retrieve from cache. |
|
149
|
|
|
* @param string $sha Object sha to retrieve from cache. |
|
150
|
|
|
* |
|
151
|
|
|
* @return stdClass|false response object if cached, false if not |
|
152
|
|
|
*/ |
|
153
|
9 |
|
protected function get( $type, $sha ) { |
|
154
|
9 |
|
if ( isset( $this->{$type}[ $sha ] ) ) { |
|
155
|
3 |
|
return $this->{$type}[ $sha ]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
6 |
|
if ( $data = get_transient( $this->cache_id( $type, $sha ) ) ) { |
|
159
|
3 |
|
return $this->{$type}[ $sha ] = $data; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
3 |
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Save data from api call by sha. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $type Object type. |
|
169
|
|
|
* @param string $sha Object sha to cache by. |
|
170
|
|
|
* @param object $data Object to cache. |
|
171
|
|
|
* @param string $time Length of time to cache object for. |
|
172
|
|
|
* |
|
173
|
|
|
* @return mixed |
|
174
|
|
|
*/ |
|
175
|
3 |
|
protected function save( $type, $sha, $data, $time ) { |
|
176
|
3 |
|
$this->{$type}[ $sha ] = $data; |
|
177
|
|
|
|
|
178
|
3 |
|
set_transient( $this->cache_id( $type, $sha ), $data, $time ); |
|
179
|
|
|
|
|
180
|
3 |
|
return $data; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Generates the cache id for a given type & sha. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $type Object type. |
|
187
|
|
|
* @param string $sha Object sha. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
9 |
|
protected function cache_id( $type, $sha ) { |
|
192
|
9 |
|
return 'wpghs_' . md5( $type . '_' . $sha ); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|