pogocache-ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,209 @@
1
+ // https://github.com/tidwall/pogocache
2
+ //
3
+ // Copyright 2025 Polypoint Labs, LLC. All rights reserved.
4
+ // This file is part of the Pogocache project.
5
+ // Use of this source code is governed by the AGPL that can be found in
6
+ // the LICENSE file.
7
+ //
8
+ // For alternative licensing options or general questions, please contact
9
+ // us at licensing@polypointlabs.com.
10
+ #ifndef POGOCACHE_H
11
+ #define POGOCACHE_H
12
+
13
+ #include <stdint.h>
14
+ #include <stdbool.h>
15
+ #include <stddef.h>
16
+
17
+ // Return values for pogocache operations
18
+ #define POGOCACHE_INSERTED 1
19
+ #define POGOCACHE_REPLACED 2
20
+ #define POGOCACHE_FOUND 3
21
+ #define POGOCACHE_NOTFOUND 4
22
+ #define POGOCACHE_DELETED 5
23
+ #define POGOCACHE_FINISHED 6
24
+ #define POGOCACHE_CANCELED 7
25
+ #define POGOCACHE_NOMEM 8
26
+
27
+ // Helper constants for ttls and expiration timestamps
28
+ #define POGOCACHE_NANOSECOND INT64_C(1)
29
+ #define POGOCACHE_MICROSECOND INT64_C(1000)
30
+ #define POGOCACHE_MILLISECOND INT64_C(1000000)
31
+ #define POGOCACHE_SECOND INT64_C(1000000000)
32
+ #define POGOCACHE_MINUTE INT64_C(60000000000)
33
+ #define POGOCACHE_HOUR INT64_C(3600000000000)
34
+
35
+ // Return values for the pogocache_iter_opts.entry callback
36
+ #define POGOCACHE_ITER_CONTINUE 0 // continue iterating
37
+ #define POGOCACHE_ITER_STOP 1 // stop iterating
38
+ #define POGOCACHE_ITER_DELETE 2 // delete current entry
39
+
40
+ // Reasons for eviction, param for the pogocache_opts.evicted callback
41
+ #define POGOCACHE_REASON_EXPIRED 1 // entry ttl has elapsed.
42
+ #define POGOCACHE_REASON_LOWMEM 2 // system is low on memory.
43
+ #define POGOCACHE_REASON_CLEARED 3 // pogocache_clear called.
44
+
45
+ struct pogocache_opts {
46
+ void *(*malloc)(size_t); // use a custom malloc function
47
+ void (*free)(void*); // use a custom free function
48
+ void (*yield)(void *udata); // contention yielder (default: no yielding)
49
+ // The 'evicted' callback is called for every entry has been evicted due
50
+ // to expiration, low memory, or when the cache is cleared. Check the
51
+ // 'reason' param for why the entry was evicted.
52
+ void (*evicted)(int shard, int reason, int64_t time, const void *key,
53
+ size_t keylen, const void *value, size_t valuelen, int64_t expires,
54
+ uint32_t flags, uint64_t cas, void *udata);
55
+ void *udata; // user data for above callbacks
56
+ // functionality options
57
+ bool usecas; // enable the compare-and-store operation
58
+ bool nosixpack; // disable sixpack key compression
59
+ bool noevict; // disable all eviction
60
+ bool allowshrink; // allow hashmap shrinking
61
+ bool usethreadbatch; // use a thread local batch (non-reentrant)
62
+ int nshards; // default 65536
63
+ int loadfactor; // default 75%
64
+ uint64_t seed; // custom hash seed, default zero
65
+ };
66
+
67
+ struct pogocache_store_opts {
68
+ int64_t time; // current time (default: use internal monotonic clock)
69
+ int64_t expires; // expiration time, nanoseconds, default: no expiration
70
+ int64_t ttl; // time-to-live, nanoseconds, default: no expiration
71
+ uint64_t cas; // CAS value (default: auto selected)
72
+ uint32_t flags; //
73
+ bool keepttl; //
74
+ bool casop; // perform the CAS operation (default: no)
75
+ bool nx; //
76
+ bool xx; //
77
+ bool lowmem; // tells the operation that the system is low on memory
78
+ // The 'entry' callback returns the value of the old entry about to be
79
+ // replaced by the new entry. This give the caller a chance to take a peek
80
+ // at the entry before it gets replaced. Return true to store the new entry
81
+ // and false to keep the old entry.
82
+ bool (*entry)(int shard, int64_t time, const void *key, size_t keylen,
83
+ const void *value, size_t valuelen, int64_t expires, uint32_t flags,
84
+ uint64_t cas, void *udata);
85
+ void *udata;
86
+ };
87
+
88
+ struct pogocache_update {
89
+ const void *value;
90
+ size_t valuelen;
91
+ uint32_t flags;
92
+ int64_t expires;
93
+ };
94
+
95
+ struct pogocache_load_opts {
96
+ int64_t time; // current time (default: use internal monotonic clock)
97
+ bool notouch; // do not update lru
98
+ // The 'entry' callback return the value of the entry. This is required to
99
+ // retreive the value of the current entry.
100
+ void (*entry)(int shard, int64_t time, const void *key, size_t keylen,
101
+ const void *value, size_t valuelen, int64_t expires, uint32_t flags,
102
+ uint64_t cas, struct pogocache_update **update, void *udata);
103
+ void *udata;
104
+ };
105
+
106
+ struct pogocache_delete_opts {
107
+ int64_t time; // current time (default: use internal monotonic clock)
108
+ // The 'entry' callback is called before deleting the entry. It provides
109
+ // all the existing entry value data.
110
+ // Return true to delete the entry or false to cancel the delete and keep
111
+ // the existing entry.
112
+ bool (*entry)(int shard, int64_t time, const void *key, size_t keylen,
113
+ const void *value, size_t valuelen, int64_t expires, uint32_t flags,
114
+ uint64_t cas, void *udata);
115
+ void *udata;
116
+ };
117
+
118
+ struct pogocache_iter_opts {
119
+ int64_t time; // current time (default: use internal monotonic clock)
120
+ bool oneshard; // only iter over one shard (default: all shards)
121
+ int oneshardidx; // index of one shard iteration, if oneshard is true.
122
+ // The 'entry' callback is called for each entry in the cache.
123
+ // Return POGOCACHE_ITER_NEXT to continue iterating
124
+ // Return POGOCACHE_ITER_STOP to stop iterating
125
+ // Return POGOCACHE_ITER_DELETE to delete the entry and continue iterating.
126
+ // It's allowed to return POGOCACHE_ITER_DELETE|POGOCACHE_ITER_STOP to
127
+ // delete the entry and stop iterating.
128
+ int (*entry)(int shard, int64_t time, const void *key, size_t keylen,
129
+ const void *value, size_t valuelen, int64_t expires, uint32_t flags,
130
+ uint64_t cas, void *udata);
131
+ void *udata;
132
+ };
133
+
134
+ struct pogocache_count_opts {
135
+ int64_t time; // current time (default: use internal monotonic clock)
136
+ bool oneshard; // only count one shard (default: all shards)
137
+ int oneshardidx; // index of one shard count, if oneshard is true.
138
+ };
139
+
140
+ struct pogocache_total_opts {
141
+ int64_t time; // current time (default: use internal monotonic clock)
142
+ bool oneshard; // only count one shard (default: all shards)
143
+ int oneshardidx; // index of one shard count, if oneshard is true.
144
+ };
145
+
146
+ struct pogocache_size_opts {
147
+ int64_t time; // current time (default: use internal monotonic clock)
148
+ bool oneshard; // only count one shard (default: all shards)
149
+ int oneshardidx; // index of one shard count, if oneshard is true.
150
+ bool entriesonly; // do not include the structure size.
151
+ };
152
+
153
+ struct pogocache_sweep_opts {
154
+ int64_t time; // current time (default: use internal monotonic clock)
155
+ bool oneshard; // only sweep one shard (default: all shards)
156
+ int oneshardidx; // index of one shard to sweep, if oneshard is true.
157
+ };
158
+
159
+ struct pogocache_clear_opts {
160
+ int64_t time; // current time (default: use internal monotonic clock)
161
+ bool oneshard; // only clear one shard (default: all shards)
162
+ int oneshardidx; // index of one shard to clear, if oneshard is true.
163
+ };
164
+
165
+ struct pogocache_sweep_poll_opts {
166
+ int64_t time; // current time (default: use internal monotonic clock)
167
+ int pollsize; // number of entries to poll (default: 20)
168
+ };
169
+
170
+ struct pogocache;
171
+
172
+ // initialize/destroy
173
+ struct pogocache *pogocache_new(struct pogocache_opts *opts);
174
+ void pogocache_free(struct pogocache *cache);
175
+
176
+ // batching
177
+ struct pogocache *pogocache_begin(struct pogocache *cache);
178
+ void pogocache_end(struct pogocache *batch);
179
+
180
+ // key operations
181
+ int pogocache_delete(struct pogocache *cache, const void *key, size_t keylen,
182
+ struct pogocache_delete_opts *dopts);
183
+ int pogocache_store(struct pogocache *cache, const void *key, size_t keylen,
184
+ const void *value, size_t valuelen, struct pogocache_store_opts *opts);
185
+ int pogocache_load(struct pogocache *cache, const void *key, size_t keylen,
186
+ struct pogocache_load_opts *opts);
187
+
188
+ // scan operations
189
+ int pogocache_iter(struct pogocache *cache, struct pogocache_iter_opts *opts);
190
+ void pogocache_sweep(struct pogocache *cache, size_t *swept, size_t *kept,
191
+ struct pogocache_sweep_opts *opts);
192
+ void pogocache_clear(struct pogocache *cache,
193
+ struct pogocache_clear_opts *opts);
194
+ double pogocache_sweep_poll(struct pogocache *cache,
195
+ struct pogocache_sweep_poll_opts *opts);
196
+
197
+ // stat operations
198
+ size_t pogocache_count(struct pogocache *cache,
199
+ struct pogocache_count_opts *opts);
200
+ uint64_t pogocache_total(struct pogocache *cache,
201
+ struct pogocache_total_opts *opts);
202
+ size_t pogocache_size(struct pogocache *cache,
203
+ struct pogocache_size_opts *opts);
204
+
205
+ // utilities
206
+ int pogocache_nshards(struct pogocache *cache);
207
+ int64_t pogocache_now(void);
208
+
209
+ #endif