-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAzureTableMemcache.php
285 lines (256 loc) · 6.35 KB
/
AzureTableMemcache.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* LICENSE: 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.
*
* PHP version 5
*
* @category Microsoft
* @package WindowsAzure\Table
* @version 1.0
* @author <[email protected]>
* @copyright 2013 pnop.inc
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link http://www.pnop.co.jp/
*/
require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Table\Models\Entity;
use WindowsAzure\Table\Models\EdmType;
/**
* This class provides Memcache interface for table
* service layer.
*
* @category Microsoft
* @package WindowsAzure\Table
* @author <[email protected]>
* @copyright 2013 pnop.inc
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @version Release: @package_version@
* @link http://www.pnop.co.jp/
*/
class AzureTableMemcache
{
/**
* @var object : Table Service object
*/
private $tableService;
/**
* @var string : Table Name
*/
private $tableName;
/**
* @var string : Value Key Name
*/
private $value;
/**
* @var array : memcache host name
*/
private $hosts;
/**
* @var array : memcache port
*/
private $ports;
/**
* @var error object
*/
public $error;
/**
* constructor
*
* @param string $accountName account name for access table storage
* @param string $accountKey key for access table storagfe
* @param string $tableName table name to access
* @param string $protocol protocol name for access table storage(http or https)
*/
function __construct($accountName, $accountKey, $tableName, $protocol='http')
{
/* connection strings */
$connectionString = 'DefaultEndpointsProtocol=' . $protocol .
';AccountName=' . $accountName .
';AccountKey=' . $accountKey;
/* connection establish */
$this->tableService = ServicesBuilder::getInstance()->createTableService($connectionString);
/* set property */
$this->tableName = $tableName;
$this->value = "value";
$this->hosts = array();
$this->ports = array();
$this->error = null;
/* create table if not exist */
try {
$this->tableService->createTable($tableName);
}
catch (ServiceException $e) {
}
}
/**
* keep compatibility with Memcache
*
* @param string $host "host name"
* @param string $port "port number"
* @return bool
*/
public function addServer($host, $port)
{
array_push($this->hosts, $host);
array_push($this->ports, $port);
return true;
}
/**
* set value (always compress off)
*
* @param string $key key
* @param string $value value
* @param int $compress always "0"
* @param int $timeout timeout (not impremented yet)
* @return bool
*/
public function set($key, $value, $compress=0, $timeout=0)
{
/* create entity */
$entity = new Entity();
$entity->setPartitionKey($key);
$entity->setRowKey($key);
/* serialize all value */
$entity->addProperty($this->value, null, serialize($value));
//Homework1: Convert Data Format To Set Expire
//$entity->addProperty("DueDate", EdmType::DATETIME, new DateTime($timeout));
/* store entity */
try {
$this->tableService->insertOrReplaceEntity($this->tableName, $entity);
return true;
}
catch (ServiceException $e) {
$this->error = $e;
return false;
}
}
/**
* get value
*
* @param string $key key
* @return mix value
*/
public function get($key)
{
/* get entity */
try {
$result = $this->tableService->getEntity($this->tableName, $key, $key);
}
catch (ServiceException $e) {
return false;
}
if ($result) {
try {
/* get value */
$entity = $result->getEntity();
$value = $entity->getProperty($this->value)->getValue();
/* return unserialized value */
return unserialize($value);
}
catch (ServiceException $e) {
$this->error = $e;
return false;
}
} else {
return false;
}
}
/**
* replace value (always compress off)
*
* @param string $key key
* @param string $value value
* @param int $compress always "0"
* @param int $timeout timeout (not impremented yet)
* @return bool
*/
public function replace($key, $value, $compress=0, $timeout=0)
{
/* create entity */
$entity = new Entity();
$entity->setPartitionKey($key);
$entity->setRowKey($key);
/* serialize all value */
$entity->addProperty($this->value, null, serialize($value));
//Homework1: Convert Data Format To Set Expire
//$entity->addProperty("DueDate", EdmType::DATETIME, new DateTime($timeout));
/* replace entity */
try {
$this->tableService->updateEntity($this->tableName, $entity);
return true;
}
catch (ServiceException $e) {
$this->error = $e;
return false;
}
}
/**
* delete value
*
* @param string $key
* @return bool
*/
function delete($key)
{
/* delete entity */
try {
$this->tableService->deleteEntity($this->tableName, $key, $key);
return true;
}
catch (ServiceException $e) {
$this->error = $e;
return false;
}
}
/**
* connect (always return TRUE)
*
* @param string $host
* @param int $port
* @param int $timeout
* @return bool
*/
function connect($host, $port=0, $timeout=0)
{
return true;
}
/**
* connect (always return TRUE)
*
* @param string $host
* @param int $port
* @param int $timeout
* @return bool
*/
function pconnect($host, $port=0, $timeout=0)
{
return true;
}
/**
* close (always return TRUE)
*
* @return bool
*/
function close()
{
return true;
}
/**
* destructor
*
* @return void
*/
function __destruct() {
}
}