-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCachePutGetExample.php
More file actions
183 lines (154 loc) · 5.88 KB
/
CachePutGetExample.php
File metadata and controls
183 lines (154 loc) · 5.88 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
* Copyright 2019 GridGain Systems, Inc. and Contributors.
*
* Licensed under the GridGain Community Edition License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
*
* 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 __DIR__ . '/../vendor/autoload.php';
use Apache\Ignite\Client;
use Apache\Ignite\ClientConfiguration;
use Apache\Ignite\Cache\CacheEntry;
use Apache\Ignite\Query\ScanQuery;
use Apache\Ignite\Exception\ClientException;
use Apache\Ignite\Data\BinaryObject;
use Apache\Ignite\Type\ObjectType;
use Apache\Ignite\Type\ComplexObjectType;
class Person
{
private static $personId = 0;
public $id;
public $firstName;
public $lastName;
public $salary;
public function __construct(string $firstName = null, string $lastName = null, float $salary = 0)
{
$this->id = Person::generateId();
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->salary = $salary;
}
public function printObject(): void
{
echo(sprintf(' %s%s', Person::class, PHP_EOL));
Person::printField('id', $this->id);
Person::printField('firstName', $this->firstName);
Person::printField('lastName', $this->lastName);
Person::printField('salary', $this->salary);
}
public static function generateId(): int
{
$id = Person::$personId;
Person::$personId++;
return $id;
}
public static function printField(string $fieldName, $fieldValue): void
{
echo(sprintf(' %s : %s%s', $fieldName, $fieldValue, PHP_EOL));
}
}
// This example demonstrates basic Cache, Key-Value Queries and Scan Query operations:
// - connects to a node
// - creates a cache, if it doesn't exist
// - specifies key type as Integer
// - executes different cache operations with Complex Objects and Binary Objects
// - put several objects
// - putAll
// - get
// - getAll
// - ScanQuery
// - destroys the cache
class CachePutGetExample
{
const ENDPOINT = '127.0.0.1:10800';
const CACHE_NAME = 'CachePutGetExample_person';
private $personCache;
private $binaryObjectCache;
private $personObjectType;
public function start(): void
{
$client = new Client();
try {
$client->connect(new ClientConfiguration(CachePutGetExample::ENDPOINT));
$this->personObjectType = (new ComplexObjectType())->
setFieldType('id', ObjectType::INTEGER);
$this->personCache = $client->getOrCreateCache(CachePutGetExample::CACHE_NAME)->
setKeyType(ObjectType::INTEGER)->
setValueType($this->personObjectType);
$this->binaryObjectCache = $client->getCache(CachePutGetExample::CACHE_NAME)->
setKeyType(ObjectType::INTEGER);
$this->putComplexObjects();
$this->putAllBinaryObjects();
$this->getAllComplexObjects();
$this->getBinaryObjects();
$this->scanQuery();
$client->destroyCache(CachePutGetExample::CACHE_NAME);
} catch (ClientException $e) {
echo('ERROR: ' . $e->getMessage() . PHP_EOL);
} finally {
$client->disconnect();
}
}
private function putComplexObjects(): void
{
$person1 = new Person('John', 'Doe', 1000);
$person2 = new Person('Jane', 'Roe', 2000);
$this->personCache->put($person1->id, $person1);
$this->personCache->put($person2->id, $person2);
echo('Complex Objects put successfully' . PHP_EOL);
}
private function putAllBinaryObjects(): void
{
// create binary object from scratch
$personBinaryObject1 = (new BinaryObject(Person::class))->
setField('id', Person::generateId(), ObjectType::INTEGER)->
setField('firstName', 'Mary')->
setField('lastName', 'Major')->
setField('salary', 1500, ObjectType::DOUBLE);
// create binary object from complex object
$personBinaryObject2 = BinaryObject::fromObject(
new Person('Richard', 'Miles', 800), $this->personObjectType);
$this->binaryObjectCache->putAll([
new CacheEntry($personBinaryObject1->getField('id'), $personBinaryObject1),
new CacheEntry($personBinaryObject2->getField('id'), $personBinaryObject2)
]);
echo('Binary Objects put successfully using putAll()' . PHP_EOL);
}
private function getAllComplexObjects(): void
{
$persons = $this->personCache->getAll([0, 1]);
echo('Complex Objects getAll:' . PHP_EOL);
foreach ($persons as $person) {
$person->getValue()->printObject();
}
}
private function getBinaryObjects(): void
{
$personBinaryObject = $this->binaryObjectCache->get(2);
echo('Binary Object get:' . PHP_EOL);
echo(sprintf(" %s%s", $personBinaryObject->getTypeName(), PHP_EOL));
foreach ($personBinaryObject->getFieldNames() as $fieldName) {
$fieldValue = $personBinaryObject->getField($fieldName);
Person::printField($fieldName, $fieldValue);
}
}
private function scanQuery(): void
{
$cursor = $this->personCache->query(new ScanQuery());
echo('Scan query results:' . PHP_EOL);
foreach ($cursor as $cacheEntry) {
$cacheEntry->getValue()->printObject();
}
}
}
$cachePutGetExample = new CachePutGetExample();
$cachePutGetExample->start();