Skip to main content

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        const USABLE_IF_ERROR = 1 << 4;
119    }
120}
121
122#[link(wasm_import_module = "fastly_cache")]
123extern "C" {
124    #[link_name = "lookup"]
125    pub fn lookup(
126        cache_key_ptr: *const u8,
127        cache_key_len: usize,
128        options_mask: CacheLookupOptionsMask,
129        options: *const CacheLookupOptions,
130        cache_handle_out: *mut CacheHandle,
131    ) -> FastlyStatus;
132
133    #[link_name = "insert"]
134    pub fn insert(
135        cache_key_ptr: *const u8,
136        cache_key_len: usize,
137        options_mask: CacheWriteOptionsMask,
138        options: *const CacheWriteOptions,
139        body_handle_out: *mut BodyHandle,
140    ) -> FastlyStatus;
141
142    #[link_name = "replace"]
143    pub fn replace(
144        cache_key_ptr: *const u8,
145        cache_key_len: usize,
146        options_mask: CacheReplaceOptionsMask,
147        options: *const CacheReplaceOptions,
148        cache_handle_out: *mut CacheHandle,
149    ) -> FastlyStatus;
150
151    #[link_name = "replace_insert"]
152    pub fn replace_insert(
153        handle: CacheReplaceHandle,
154        options_mask: CacheWriteOptionsMask,
155        options: *const CacheWriteOptions,
156        body_handle_out: *mut BodyHandle,
157    ) -> FastlyStatus;
158
159    #[link_name = "replace_get_age_ns"]
160    pub fn replace_get_age_ns(
161        handle: CacheReplaceHandle,
162        duration_out: *mut CacheDurationNs,
163    ) -> FastlyStatus;
164
165    #[link_name = "replace_get_body"]
166    pub fn replace_get_body(
167        handle: CacheReplaceHandle,
168        options_mask: CacheGetBodyOptionsMask,
169        options: *const CacheGetBodyOptions,
170        body_handle_out: *mut BodyHandle,
171    ) -> FastlyStatus;
172
173    #[link_name = "replace_get_hits"]
174    pub fn replace_get_hits(
175        handle: CacheReplaceHandle,
176        hits_out: *mut CacheHitCount,
177    ) -> FastlyStatus;
178
179    #[link_name = "replace_get_length"]
180    pub fn replace_get_length(
181        handle: CacheReplaceHandle,
182        length_out: *mut CacheObjectLength,
183    ) -> FastlyStatus;
184
185    #[link_name = "replace_get_max_age_ns"]
186    pub fn replace_get_max_age_ns(
187        handle: CacheReplaceHandle,
188        duration_out: *mut CacheDurationNs,
189    ) -> FastlyStatus;
190
191    #[link_name = "replace_get_stale_while_revalidate_ns"]
192    pub fn replace_get_stale_while_revalidate_ns(
193        handle: CacheReplaceHandle,
194        duration_out: *mut CacheDurationNs,
195    ) -> FastlyStatus;
196
197    #[link_name = "replace_get_state"]
198    pub fn replace_get_state(
199        handle: CacheReplaceHandle,
200        cache_lookup_state_out: *mut CacheLookupState,
201    ) -> FastlyStatus;
202
203    #[link_name = "replace_get_user_metadata"]
204    pub fn replace_get_user_metadata(
205        handle: CacheReplaceHandle,
206        user_metadata_out_ptr: *mut u8,
207        user_metadata_out_len: usize,
208        nwritten_out: *mut usize,
209    ) -> FastlyStatus;
210
211    #[link_name = "transaction_lookup"]
212    pub fn transaction_lookup(
213        cache_key_ptr: *const u8,
214        cache_key_len: usize,
215        options_mask: CacheLookupOptionsMask,
216        options: *const CacheLookupOptions,
217        cache_handle_out: *mut CacheHandle,
218    ) -> FastlyStatus;
219
220    #[link_name = "transaction_lookup_async"]
221    pub fn transaction_lookup_async(
222        cache_key_ptr: *const u8,
223        cache_key_len: usize,
224        options_mask: CacheLookupOptionsMask,
225        options: *const CacheLookupOptions,
226        busy_handle_out: *mut CacheBusyHandle,
227    ) -> FastlyStatus;
228
229    #[link_name = "cache_busy_handle_wait"]
230    pub fn cache_busy_handle_wait(
231        busy_handle: CacheBusyHandle,
232        cache_handle_out: *mut CacheHandle,
233    ) -> FastlyStatus;
234
235    #[link_name = "transaction_insert"]
236    pub fn transaction_insert(
237        handle: CacheHandle,
238        options_mask: CacheWriteOptionsMask,
239        options: *const CacheWriteOptions,
240        body_handle_out: *mut BodyHandle,
241    ) -> FastlyStatus;
242
243    #[link_name = "transaction_insert_and_stream_back"]
244    pub fn transaction_insert_and_stream_back(
245        handle: CacheHandle,
246        options_mask: CacheWriteOptionsMask,
247        options: *const CacheWriteOptions,
248        body_handle_out: *mut BodyHandle,
249        cache_handle_out: *mut CacheHandle,
250    ) -> FastlyStatus;
251
252    #[link_name = "transaction_update"]
253    pub fn transaction_update(
254        handle: CacheHandle,
255        options_mask: CacheWriteOptionsMask,
256        options: *const CacheWriteOptions,
257    ) -> FastlyStatus;
258
259    #[link_name = "transaction_cancel"]
260    pub fn transaction_cancel(handle: CacheHandle) -> FastlyStatus;
261
262    #[link_name = "close"]
263    pub fn close(handle: CacheHandle) -> FastlyStatus;
264
265    #[link_name = "close_busy"]
266    pub fn close_busy(handle: CacheBusyHandle) -> FastlyStatus;
267
268    #[link_name = "get_state"]
269    pub fn get_state(
270        handle: CacheHandle,
271        cache_lookup_state_out: *mut CacheLookupState,
272    ) -> FastlyStatus;
273
274    #[link_name = "get_user_metadata"]
275    pub fn get_user_metadata(
276        handle: CacheHandle,
277        user_metadata_out_ptr: *mut u8,
278        user_metadata_out_len: usize,
279        nwritten_out: *mut usize,
280    ) -> FastlyStatus;
281
282    #[link_name = "get_body"]
283    pub fn get_body(
284        handle: CacheHandle,
285        options_mask: CacheGetBodyOptionsMask,
286        options: *const CacheGetBodyOptions,
287        body_handle_out: *mut BodyHandle,
288    ) -> FastlyStatus;
289
290    #[link_name = "get_length"]
291    pub fn get_length(handle: CacheHandle, length_out: *mut CacheObjectLength) -> FastlyStatus;
292
293    #[link_name = "get_max_age_ns"]
294    pub fn get_max_age_ns(handle: CacheHandle, duration_out: *mut CacheDurationNs) -> FastlyStatus;
295
296    #[link_name = "get_stale_while_revalidate_ns"]
297    pub fn get_stale_while_revalidate_ns(
298        handle: CacheHandle,
299        duration_out: *mut CacheDurationNs,
300    ) -> FastlyStatus;
301
302    #[link_name = "get_age_ns"]
303    pub fn get_age_ns(handle: CacheHandle, duration_out: *mut CacheDurationNs) -> FastlyStatus;
304
305    #[link_name = "get_hits"]
306    pub fn get_hits(handle: CacheHandle, hits_out: *mut CacheHitCount) -> FastlyStatus;
307}