fastly_sys/
fastly_cache.rs

1use fastly_shared::FastlyStatus;
2
3use crate::{BodyHandle, RequestHandle};
4
5pub type CacheHandle = u32;
6pub type CacheBusyHandle = u32;
7pub type CacheReplaceHandle = u32;
8
9pub type CacheObjectLength = u64;
10pub type CacheDurationNs = u64;
11pub type CacheHitCount = u64;
12pub type Boolean = u8;
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15#[repr(C)]
16pub struct CacheLookupOptions {
17    pub request_headers: RequestHandle,
18    pub service: *const u8,
19    pub service_len: u32,
20}
21
22bitflags::bitflags! {
23    #[repr(transparent)]
24    pub struct CacheLookupOptionsMask: u32 {
25        const _RESERVED = 1 << 0;
26        const REQUEST_HEADERS = 1 << 1;
27        const SERVICE = 1 << 2;
28        const ALWAYS_USE_REQUESTED_RANGE = 1 << 3;
29    }
30}
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33#[repr(C)]
34pub struct CacheReplaceOptions {
35    pub request_headers: RequestHandle,
36    pub replace_strategy: u32,
37    pub service: *const u8,
38    pub service_len: u32,
39}
40
41bitflags::bitflags! {
42    #[repr(transparent)]
43    pub struct CacheReplaceOptionsMask: u32 {
44        const _RESERVED = 1 << 0;
45        const REQUEST_HEADERS = 1 << 1;
46        const REPLACE_STRATEGY = 1 << 2;
47        const SERVICE = 1 << 3;
48        const ALWAYS_USE_REQUESTED_RANGE= 1 << 4;
49    }
50}
51
52#[repr(u32)]
53pub enum CacheReplaceStrategy {
54    Immediate = 1,
55    ImmediateForceMiss = 2,
56    Wait = 3,
57}
58
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60#[repr(C)]
61pub struct CacheWriteOptions {
62    pub max_age_ns: u64,
63    pub request_headers: RequestHandle,
64    pub vary_rule_ptr: *const u8,
65    pub vary_rule_len: usize,
66    pub initial_age_ns: u64,
67    pub stale_while_revalidate_ns: u64,
68    pub surrogate_keys_ptr: *const u8,
69    pub surrogate_keys_len: usize,
70    pub length: CacheObjectLength,
71    pub user_metadata_ptr: *const u8,
72    pub user_metadata_len: usize,
73    pub edge_max_age_ns: u64,
74    pub service: *const u8,
75    pub service_len: u32,
76}
77
78bitflags::bitflags! {
79    #[repr(transparent)]
80    pub struct CacheWriteOptionsMask: u32 {
81        const _RESERVED = 1 << 0;
82        const REQUEST_HEADERS = 1 << 1;
83        const VARY_RULE = 1 << 2;
84        const INITIAL_AGE_NS = 1 << 3;
85        const STALE_WHILE_REVALIDATE_NS = 1 << 4;
86        const SURROGATE_KEYS = 1 << 5;
87        const LENGTH = 1 << 6;
88        const USER_METADATA = 1 << 7;
89        const SENSITIVE_DATA = 1 << 8;
90        const EDGE_MAX_AGE_NS = 1 << 9;
91        const SERVICE = 1 << 10;
92    }
93}
94
95#[derive(Clone, Copy, Debug, PartialEq, Eq)]
96#[repr(C)]
97pub struct CacheGetBodyOptions {
98    pub from: u64,
99    pub to: u64,
100}
101
102bitflags::bitflags! {
103    #[repr(transparent)]
104    pub struct CacheGetBodyOptionsMask: u32 {
105        const _RESERVED = 1 << 0;
106        const FROM = 1 << 1;
107        const TO = 1 << 2;
108    }
109}
110
111bitflags::bitflags! {
112    #[repr(transparent)]
113    pub struct CacheLookupState: u32 {
114        const FOUND = 1 << 0;
115        const USABLE = 1 << 1;
116        const STALE = 1 << 2;
117        const MUST_INSERT_OR_UPDATE = 1 << 3;
118    }
119}
120
121#[link(wasm_import_module = "fastly_cache")]
122extern "C" {
123    #[link_name = "lookup"]
124    pub fn lookup(
125        cache_key_ptr: *const u8,
126        cache_key_len: usize,
127        options_mask: CacheLookupOptionsMask,
128        options: *const CacheLookupOptions,
129        cache_handle_out: *mut CacheHandle,
130    ) -> FastlyStatus;
131
132    #[link_name = "insert"]
133    pub fn insert(
134        cache_key_ptr: *const u8,
135        cache_key_len: usize,
136        options_mask: CacheWriteOptionsMask,
137        options: *const CacheWriteOptions,
138        body_handle_out: *mut BodyHandle,
139    ) -> FastlyStatus;
140
141    #[link_name = "replace"]
142    pub fn replace(
143        cache_key_ptr: *const u8,
144        cache_key_len: usize,
145        options_mask: CacheReplaceOptionsMask,
146        options: *const CacheReplaceOptions,
147        cache_handle_out: *mut CacheHandle,
148    ) -> FastlyStatus;
149
150    #[link_name = "replace_insert"]
151    pub fn replace_insert(
152        handle: CacheReplaceHandle,
153        options_mask: CacheWriteOptionsMask,
154        options: *const CacheWriteOptions,
155        body_handle_out: *mut BodyHandle,
156    ) -> FastlyStatus;
157
158    #[link_name = "replace_get_age_ns"]
159    pub fn replace_get_age_ns(
160        handle: CacheReplaceHandle,
161        duration_out: *mut CacheDurationNs,
162    ) -> FastlyStatus;
163
164    #[link_name = "replace_get_body"]
165    pub fn replace_get_body(
166        handle: CacheReplaceHandle,
167        options_mask: CacheGetBodyOptionsMask,
168        options: *const CacheGetBodyOptions,
169        body_handle_out: *mut BodyHandle,
170    ) -> FastlyStatus;
171
172    #[link_name = "replace_get_hits"]
173    pub fn replace_get_hits(
174        handle: CacheReplaceHandle,
175        hits_out: *mut CacheHitCount,
176    ) -> FastlyStatus;
177
178    #[link_name = "replace_get_length"]
179    pub fn replace_get_length(
180        handle: CacheReplaceHandle,
181        length_out: *mut CacheObjectLength,
182    ) -> FastlyStatus;
183
184    #[link_name = "replace_get_max_age_ns"]
185    pub fn replace_get_max_age_ns(
186        handle: CacheReplaceHandle,
187        duration_out: *mut CacheDurationNs,
188    ) -> FastlyStatus;
189
190    #[link_name = "replace_get_stale_while_revalidate_ns"]
191    pub fn replace_get_stale_while_revalidate_ns(
192        handle: CacheReplaceHandle,
193        duration_out: *mut CacheDurationNs,
194    ) -> FastlyStatus;
195
196    #[link_name = "replace_get_state"]
197    pub fn replace_get_state(
198        handle: CacheReplaceHandle,
199        cache_lookup_state_out: *mut CacheLookupState,
200    ) -> FastlyStatus;
201
202    #[link_name = "replace_get_user_metadata"]
203    pub fn replace_get_user_metadata(
204        handle: CacheReplaceHandle,
205        user_metadata_out_ptr: *mut u8,
206        user_metadata_out_len: usize,
207        nwritten_out: *mut usize,
208    ) -> FastlyStatus;
209
210    #[link_name = "transaction_lookup"]
211    pub fn transaction_lookup(
212        cache_key_ptr: *const u8,
213        cache_key_len: usize,
214        options_mask: CacheLookupOptionsMask,
215        options: *const CacheLookupOptions,
216        cache_handle_out: *mut CacheHandle,
217    ) -> FastlyStatus;
218
219    #[link_name = "transaction_lookup_async"]
220    pub fn transaction_lookup_async(
221        cache_key_ptr: *const u8,
222        cache_key_len: usize,
223        options_mask: CacheLookupOptionsMask,
224        options: *const CacheLookupOptions,
225        busy_handle_out: *mut CacheBusyHandle,
226    ) -> FastlyStatus;
227
228    #[link_name = "cache_busy_handle_wait"]
229    pub fn cache_busy_handle_wait(
230        busy_handle: CacheBusyHandle,
231        cache_handle_out: *mut CacheHandle,
232    ) -> FastlyStatus;
233
234    #[link_name = "transaction_insert"]
235    pub fn transaction_insert(
236        handle: CacheHandle,
237        options_mask: CacheWriteOptionsMask,
238        options: *const CacheWriteOptions,
239        body_handle_out: *mut BodyHandle,
240    ) -> FastlyStatus;
241
242    #[link_name = "transaction_insert_and_stream_back"]
243    pub fn transaction_insert_and_stream_back(
244        handle: CacheHandle,
245        options_mask: CacheWriteOptionsMask,
246        options: *const CacheWriteOptions,
247        body_handle_out: *mut BodyHandle,
248        cache_handle_out: *mut CacheHandle,
249    ) -> FastlyStatus;
250
251    #[link_name = "transaction_update"]
252    pub fn transaction_update(
253        handle: CacheHandle,
254        options_mask: CacheWriteOptionsMask,
255        options: *const CacheWriteOptions,
256    ) -> FastlyStatus;
257
258    #[link_name = "transaction_cancel"]
259    pub fn transaction_cancel(handle: CacheHandle) -> FastlyStatus;
260
261    #[link_name = "close"]
262    pub fn close(handle: CacheHandle) -> FastlyStatus;
263
264    #[link_name = "close_busy"]
265    pub fn close_busy(handle: CacheBusyHandle) -> FastlyStatus;
266
267    #[link_name = "get_state"]
268    pub fn get_state(
269        handle: CacheHandle,
270        cache_lookup_state_out: *mut CacheLookupState,
271    ) -> FastlyStatus;
272
273    #[link_name = "get_user_metadata"]
274    pub fn get_user_metadata(
275        handle: CacheHandle,
276        user_metadata_out_ptr: *mut u8,
277        user_metadata_out_len: usize,
278        nwritten_out: *mut usize,
279    ) -> FastlyStatus;
280
281    #[link_name = "get_body"]
282    pub fn get_body(
283        handle: CacheHandle,
284        options_mask: CacheGetBodyOptionsMask,
285        options: *const CacheGetBodyOptions,
286        body_handle_out: *mut BodyHandle,
287    ) -> FastlyStatus;
288
289    #[link_name = "get_length"]
290    pub fn get_length(handle: CacheHandle, length_out: *mut CacheObjectLength) -> FastlyStatus;
291
292    #[link_name = "get_max_age_ns"]
293    pub fn get_max_age_ns(handle: CacheHandle, duration_out: *mut CacheDurationNs) -> FastlyStatus;
294
295    #[link_name = "get_stale_while_revalidate_ns"]
296    pub fn get_stale_while_revalidate_ns(
297        handle: CacheHandle,
298        duration_out: *mut CacheDurationNs,
299    ) -> FastlyStatus;
300
301    #[link_name = "get_age_ns"]
302    pub fn get_age_ns(handle: CacheHandle, duration_out: *mut CacheDurationNs) -> FastlyStatus;
303
304    #[link_name = "get_hits"]
305    pub fn get_hits(handle: CacheHandle, hits_out: *mut CacheHitCount) -> FastlyStatus;
306}