Skip to content

Commit 52c6ce1

Browse files
chore: add initial docs for globe kv
1 parent 1048d45 commit 52c6ce1

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

docs/storage/index.mdx

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
title: GlobeKV
3+
---
4+
5+
GlobeKV is a **key-value store** designed for **fast reads** and **low-latency data access**. It is ideal for **caching, user settings, session storage, and feature flags** in Flutter applications.
6+
7+
Unlike traditional databases, GlobeKV **does not guarantee immediate consistency**. Updates can take up to **60 seconds** to propagate globally. This makes it great for **temporary or frequently accessed data** but unsuitable for operations requiring absolute consistency (e.g., payments, counters).
8+
9+
This guide explains **how to use GlobeKV**, covering **setup, storing and retrieving data, expiration, performance, and eventual consistency**. For method details and advanced configurations, links to the API documentation are provided where relevant.
10+
11+
## Setting Up GlobeKV
12+
13+
### Adding Globe to your Project
14+
15+
Add `globe_kv` to your `pubspec.yaml`:
16+
17+
```yaml
18+
dependencies:
19+
globe_kv: latest
20+
```
21+
22+
Then install it:
23+
24+
```bash
25+
dart pub get
26+
```
27+
28+
Or run this command in your termina:
29+
30+
With Dart:
31+
32+
```bash
33+
dart pub add globe_kv
34+
```
35+
36+
With Flutter:
37+
38+
```bash
39+
flutter pub add globe_kv
40+
```
41+
42+
### Creating a Namespace
43+
44+
To use GlobeKV, you need a **KV namespace**.
45+
Replace with steps
46+
47+
### Creating an Instance
48+
49+
Create a new GlobeKV instance, passing in your KV namespace.
50+
51+
```dart
52+
import 'package:globe_kv/globe_kv.dart';
53+
// Replace 'namespace-id' with your actual KV namespace
54+
final kv = GlobeKV('namespace-id');
55+
```
56+
57+
### Persist Data to Your Local Filesystem
58+
59+
In development, GlobeKV will use in-memory storage so your data will be lost when the Dart app is restarted. To persist data to your local filesystem, use the GlobeFileStore instance:
60+
61+
```dart
62+
final kv = GlobeKV('namespace-id', store: GlobeFileStore());
63+
```
64+
65+
## Using GlobeKV
66+
67+
### Storing, Retrieving, and Deleting Data
68+
69+
GlobeKV is a **key-value store**, meaning you store and retrieve data using **keys**.
70+
71+
#### Writing Data
72+
73+
```dart
74+
await kv.set('user:123', 'John Doe');
75+
```
76+
77+
#### Reading Data
78+
79+
```dart
80+
final userData = await kv.getString('user:123');
81+
debugPrint(userData); // Output: John Doe
82+
```
83+
84+
#### Deleting Data
85+
86+
```dart
87+
await kv.delete('user:123');
88+
```
89+
90+
#### Retrieving Data
91+
92+
GlobeKV **supports typed retrieval**, so you can fetch data in a specific format:
93+
94+
```dart
95+
await kv.getString('key'); // String?
96+
await kv.getInt('key'); // int?
97+
await kv.getBool('key'); // bool?
98+
await kv.getBinary('key'); // List<int>?
99+
```
100+
101+
### Listing and Filtering Data
102+
103+
When storing multiple related keys (e.g., user data), use prefixes to **group and filter keys**.
104+
105+
#### Listing All Keys with a Prefix
106+
107+
```dart
108+
final result = await kv.list(prefix: 'user:');
109+
print(result.results.map((e) => e.key)); // ['user:123', 'user:456']
110+
```
111+
112+
#### Paginating Large Datasets
113+
114+
By default, list() returns **up to 1000 keys per page**. If you have more, paginate:
115+
116+
```dart
117+
final result = await kv.list(prefix: 'user:', limit: 10);
118+
if (!result.complete) {
119+
final nextResult = await kv.list(prefix: 'user:', limit: 10, cursor: result.cursor!);
120+
}
121+
```
122+
123+
### Expiring Data (TTL & Absolute Expiration)
124+
125+
GlobeKV supports **automatic expiration**, useful for caching API responses or managing temporary session data.
126+
127+
#### Setting a Time-to-Live (TTL)
128+
129+
```dart
130+
await kv.set('user:123', 'John Doe', ttl: 5); // Expires in 5 seconds
131+
```
132+
133+
#### Setting an Absolute Expiration Time
134+
135+
```dart
136+
await kv.set('user:123', 'John Doe', expires: DateTime.now().add(Duration(minutes: 5)));
137+
```
138+
139+
#### When to Use TTL vs Expiration
140+
141+
- TTL (time-to-live) is best for **short-lived cache** or session data.
142+
- **Absolute expiration** is best when data must expire at a fixed point in time.
143+
144+
### Performance: Hot vs Cold Reads
145+
146+
#### Understanding Edge Caching
147+
148+
GlobeKV uses **edge locations** (Points of Presence, POPs) to **cache data closer to users**.
149+
150+
- If data is **already cached** at an edge location → **Hot Read** (Fast)
151+
- If data **needs to be fetched** from storage → **Cold Read** (Slower)
152+
153+
By default, **hot reads last 60 seconds**.
154+
155+
#### Increasing Cache Duration for Faster Reads
156+
157+
```dart
158+
await kv.get('user:123', ttl: 60 * 60 * 24); // Cache for 24 hours
159+
```
160+
161+
Use this for data that rarely changes, like feature flags or app settings.
162+
163+
### Eventual Consistency
164+
165+
GlobeKV is **not fully consistent**—updates may take **up to 60 seconds** to reflect globally.
166+
167+
#### Good for:
168+
169+
- User preferences
170+
- Feature flags
171+
- Non-critical cached data
172+
173+
#### Not suitable for:
174+
175+
- Financial transactions
176+
- Real-time counters
177+
178+
#### For Less Frequent Updates, Use a Longer Cache
179+
180+
If your data doesn't change often, increasing the **TTL on reads** can improve performance:
181+
182+
```dart
183+
await kv.get('user:123', ttl: 60 * 60 * 24); // Cache for 24 hours
184+
```
185+
186+
This reduces cold reads and improves performance for users.

0 commit comments

Comments
 (0)