Repository navigation
Expand file tree
/
Copy path01-basic.php
More file actions
104 lines (75 loc) · 2.5 KB
/
Copy path01-basic.php
File metadata and controls
104 lines (75 loc) · 2.5 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
<?php
/**
* Key-Value Store - distributed key-value store built on JetStream.
*
* Supports: CRUD, revisions (CAS), history, watch, TTL.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
use Sofascore\Nats\Connection;
use Sofascore\Nats\KeyValue\KeyValueConfig;
$conn = Connection::connect();
$js = $conn->jetStream();
// --- Creating a bucket ---
$kv = $js->createKeyValue(new KeyValueConfig(
bucket: 'settings',
description: 'Application settings',
history: 5, // Keeps last 5 revisions
ttl: 3600.0, // 1h TTL
maxValueSize: 1024, // Max 1KB per value
replicas: 1,
));
// --- Put / Get ---
$revision = $kv->put('theme', 'dark');
echo "Put 'theme' = 'dark', revision: {$revision}\n";
$entry = $kv->get('theme');
echo "Get 'theme': value={$entry->value}, revision={$entry->revision}\n";
echo " bucket={$entry->bucket}, operation={$entry->operation->value}\n";
echo " timestamp={$entry->timestamp->format('c')}\n";
// --- Create (fails if key exists) ---
try {
$rev = $kv->create('language', 'hr');
echo "Created 'language', revision: {$rev}\n";
} catch (\Sofascore\Nats\NatsException $e) {
echo "Create failed: {$e->getMessage()}\n";
}
// --- Update (CAS - Compare And Swap) ---
$currentEntry = $kv->get('theme');
$newRev = $kv->update('theme', 'light', $currentEntry->revision);
echo "Updated 'theme' to 'light', new revision: {$newRev}\n";
// CAS failure: old revision
try {
$kv->update('theme', 'blue', $currentEntry->revision); // Old revision!
} catch (\Sofascore\Nats\NatsException $e) {
echo "CAS conflict: {$e->getMessage()}\n";
}
// --- Delete ---
$kv->delete('language');
// Delete with revision (conditional)
// $kv->delete('theme', lastRevision: $newRev);
// --- Purge (deletes all revisions of a key) ---
$kv->purge('theme');
// --- History ---
$kv->put('counter', '1');
$kv->put('counter', '2');
$kv->put('counter', '3');
$history = $kv->history('counter');
foreach ($history as $entry) {
echo "History: rev={$entry->revision}, value={$entry->value}, op={$entry->operation->value}\n";
}
// --- List keys ---
$kv->put('a', '1');
$kv->put('b', '2');
$kv->put('c', '3');
foreach ($kv->listKeys() as $key) {
echo "Key: {$key}\n";
}
// --- Status ---
$status = $kv->status();
echo "Bucket: {$status->bucket}\n";
echo "Values: {$status->values}\n";
echo "Bytes: {$status->bytes}\n";
echo "History: {$status->history}\n";
echo "Backing store: {$status->backingStore}\n";
// --- Cleanup ---
$js->deleteKeyValue('settings');
$conn->close();