Skip to main content

gestalt/
cache.rs

1// Code generated by sdkgen. DO NOT EDIT.
2
3//! Generated native types and clients for cache.proto.
4
5use crate::codec::cache::{
6    from_wire_cache_delete_many_response, from_wire_cache_delete_response,
7    from_wire_cache_get_many_response, from_wire_cache_get_response,
8    from_wire_cache_touch_response, to_wire_cache_delete_many_request,
9    to_wire_cache_delete_request, to_wire_cache_get_many_request, to_wire_cache_get_request,
10    to_wire_cache_set_many_request, to_wire_cache_set_request, to_wire_cache_touch_request,
11};
12use crate::codec::host_service::{HostServiceChannel, connect_host_service, plain_channel};
13use crate::generated::v1;
14use crate::rpc_support::GestaltError;
15
16/// CacheDeleteManyRequest removes multiple cache keys in one RPC.
17///
18/// Native message type for `gestalt.provider.v1.CacheDeleteManyRequest`.
19#[derive(Clone, Debug, Default, PartialEq)]
20pub struct CacheDeleteManyRequest {
21    /// The `keys` field.
22    pub keys: Vec<String>,
23}
24
25/// CacheDeleteManyResponse reports how many keys were deleted.
26///
27/// Native message type for `gestalt.provider.v1.CacheDeleteManyResponse`.
28#[derive(Clone, Debug, Default, PartialEq)]
29pub struct CacheDeleteManyResponse {
30    /// The `deleted` field.
31    pub deleted: i64,
32}
33
34/// CacheDeleteRequest removes one cache key.
35///
36/// Native message type for `gestalt.provider.v1.CacheDeleteRequest`.
37#[derive(Clone, Debug, Default, PartialEq)]
38pub struct CacheDeleteRequest {
39    /// The `key` field.
40    pub key: String,
41}
42
43/// CacheDeleteResponse reports whether one key existed and was deleted.
44///
45/// Native message type for `gestalt.provider.v1.CacheDeleteResponse`.
46#[derive(Clone, Debug, Default, PartialEq)]
47pub struct CacheDeleteResponse {
48    /// The `deleted` field.
49    pub deleted: bool,
50}
51
52/// CacheGetManyRequest loads multiple cache keys in one RPC.
53///
54/// Native message type for `gestalt.provider.v1.CacheGetManyRequest`.
55#[derive(Clone, Debug, Default, PartialEq)]
56pub struct CacheGetManyRequest {
57    /// The `keys` field.
58    pub keys: Vec<String>,
59}
60
61/// CacheGetManyResponse returns every lookup result for GetMany.
62///
63/// Native message type for `gestalt.provider.v1.CacheGetManyResponse`.
64#[derive(Clone, Debug, Default, PartialEq)]
65pub struct CacheGetManyResponse {
66    /// The `entries` field.
67    pub entries: Vec<CacheResult>,
68}
69
70/// CacheGetRequest loads one cache key.
71///
72/// Native message type for `gestalt.provider.v1.CacheGetRequest`.
73#[derive(Clone, Debug, Default, PartialEq)]
74pub struct CacheGetRequest {
75    /// The `key` field.
76    pub key: String,
77}
78
79/// CacheGetResponse is the result of looking up one cache key.
80///
81/// Native message type for `gestalt.provider.v1.CacheGetResponse`.
82#[derive(Clone, Debug, Default, PartialEq)]
83pub struct CacheGetResponse {
84    /// The `found` field.
85    pub found: bool,
86    /// The `value` field.
87    pub value: Vec<u8>,
88}
89
90/// CacheResult is one lookup result returned by GetMany.
91///
92/// Native message type for `gestalt.provider.v1.CacheResult`.
93#[derive(Clone, Debug, Default, PartialEq)]
94pub struct CacheResult {
95    /// The `key` field.
96    pub key: String,
97    /// The `found` field.
98    pub found: bool,
99    /// The `value` field.
100    pub value: Vec<u8>,
101}
102
103/// CacheSetEntry is one key/value pair written by SetMany.
104///
105/// Native message type for `gestalt.provider.v1.CacheSetEntry`.
106#[derive(Clone, Debug, Default, PartialEq)]
107pub struct CacheSetEntry {
108    /// The `key` field.
109    pub key: String,
110    /// The `value` field.
111    pub value: Vec<u8>,
112}
113
114/// CacheSetManyRequest writes multiple cache keys in one RPC.
115///
116/// Native message type for `gestalt.provider.v1.CacheSetManyRequest`.
117#[derive(Clone, Debug, Default, PartialEq)]
118pub struct CacheSetManyRequest {
119    /// The `entries` field.
120    pub entries: Vec<CacheSetEntry>,
121    /// The `ttl` field; None when unset.
122    pub ttl: Option<std::time::Duration>,
123}
124
125/// CacheSetRequest writes one cache key.
126///
127/// Native message type for `gestalt.provider.v1.CacheSetRequest`.
128#[derive(Clone, Debug, Default, PartialEq)]
129pub struct CacheSetRequest {
130    /// The `key` field.
131    pub key: String,
132    /// The `value` field.
133    pub value: Vec<u8>,
134    /// ttl applies an optional expiration to the entry.
135    ///
136    /// The `ttl` field; None when unset.
137    pub ttl: Option<std::time::Duration>,
138}
139
140/// CacheTouchRequest extends the TTL for one cache key.
141///
142/// Native message type for `gestalt.provider.v1.CacheTouchRequest`.
143#[derive(Clone, Debug, Default, PartialEq)]
144pub struct CacheTouchRequest {
145    /// The `key` field.
146    pub key: String,
147    /// The `ttl` field; None when unset.
148    pub ttl: Option<std::time::Duration>,
149}
150
151/// CacheTouchResponse reports whether a key existed and had its TTL updated.
152///
153/// Native message type for `gestalt.provider.v1.CacheTouchResponse`.
154#[derive(Clone, Debug, Default, PartialEq)]
155pub struct CacheTouchResponse {
156    /// The `touched` field.
157    pub touched: bool,
158}
159
160/// Cache models the shared Gestalt cache-provider protocol.
161///
162/// Client for the `gestalt.provider.v1.Cache` service.
163pub struct Cache {
164    inner: v1::cache_client::CacheClient<HostServiceChannel>,
165    timeout: Option<std::time::Duration>,
166}
167
168impl Cache {
169    /// Creates a client over an established channel.
170    pub fn new(channel: tonic::transport::Channel) -> Self {
171        Self {
172            inner: v1::cache_client::CacheClient::new(plain_channel(channel)),
173            timeout: None,
174        }
175    }
176
177    /// Sets a deadline applied to every unary call; calls that run past it
178    /// fail with DEADLINE_EXCEEDED. Streaming calls are unaffected.
179    pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
180        self.timeout = Some(timeout);
181        self
182    }
183
184    /// Connects to the `cache` host service described by the environment.
185    pub async fn connect() -> Result<Self, GestaltError> {
186        Self::connect_named("").await
187    }
188
189    /// Connects to the named `cache` host-service binding.
190    pub async fn connect_named(name: &str) -> Result<Self, GestaltError> {
191        Ok(Self {
192            inner: v1::cache_client::CacheClient::new(connect_host_service("cache", name).await?),
193            timeout: None,
194        })
195    }
196
197    /// Calls `gestalt.provider.v1.Cache.Get`.
198    pub async fn get(&mut self, key: String) -> Result<Option<Vec<u8>>, GestaltError> {
199        let request = CacheGetRequest { key };
200        let mut tonic_request = tonic::Request::new(to_wire_cache_get_request(request));
201        if let Some(timeout) = self.timeout {
202            tonic_request.set_timeout(timeout);
203        }
204        let response =
205            from_wire_cache_get_response(self.inner.get(tonic_request).await?.into_inner());
206        if !response.found {
207            return Ok(None);
208        }
209        Ok(Some(response.value))
210    }
211
212    /// Calls `gestalt.provider.v1.Cache.Get` with the full request and response messages.
213    pub async fn get_raw(
214        &mut self,
215        request: CacheGetRequest,
216    ) -> Result<CacheGetResponse, GestaltError> {
217        let mut tonic_request = tonic::Request::new(to_wire_cache_get_request(request));
218        if let Some(timeout) = self.timeout {
219            tonic_request.set_timeout(timeout);
220        }
221        let response = self.inner.get(tonic_request).await?;
222        Ok(from_wire_cache_get_response(response.into_inner()))
223    }
224
225    /// Calls `gestalt.provider.v1.Cache.GetMany`.
226    pub async fn get_many(
227        &mut self,
228        keys: Vec<String>,
229    ) -> Result<std::collections::BTreeMap<String, Vec<u8>>, GestaltError> {
230        let request = CacheGetManyRequest { keys };
231        let mut tonic_request = tonic::Request::new(to_wire_cache_get_many_request(request));
232        if let Some(timeout) = self.timeout {
233            tonic_request.set_timeout(timeout);
234        }
235        let response = from_wire_cache_get_many_response(
236            self.inner.get_many(tonic_request).await?.into_inner(),
237        );
238        let mut out = std::collections::BTreeMap::new();
239        for entry in response.entries {
240            if entry.found {
241                out.insert(entry.key, entry.value);
242            }
243        }
244        Ok(out)
245    }
246
247    /// Calls `gestalt.provider.v1.Cache.GetMany` with the full request and response messages.
248    pub async fn get_many_raw(
249        &mut self,
250        request: CacheGetManyRequest,
251    ) -> Result<CacheGetManyResponse, GestaltError> {
252        let mut tonic_request = tonic::Request::new(to_wire_cache_get_many_request(request));
253        if let Some(timeout) = self.timeout {
254            tonic_request.set_timeout(timeout);
255        }
256        let response = self.inner.get_many(tonic_request).await?;
257        Ok(from_wire_cache_get_many_response(response.into_inner()))
258    }
259
260    /// Calls `gestalt.provider.v1.Cache.Set`.
261    pub async fn set(
262        &mut self,
263        key: String,
264        value: Vec<u8>,
265        ttl: Option<std::time::Duration>,
266    ) -> Result<(), GestaltError> {
267        let request = CacheSetRequest { key, value, ttl };
268        let mut tonic_request = tonic::Request::new(to_wire_cache_set_request(request));
269        if let Some(timeout) = self.timeout {
270            tonic_request.set_timeout(timeout);
271        }
272        self.inner.set(tonic_request).await?;
273        Ok(())
274    }
275
276    /// Calls `gestalt.provider.v1.Cache.Set` with the full request and response messages.
277    pub async fn set_raw(&mut self, request: CacheSetRequest) -> Result<(), GestaltError> {
278        let mut tonic_request = tonic::Request::new(to_wire_cache_set_request(request));
279        if let Some(timeout) = self.timeout {
280            tonic_request.set_timeout(timeout);
281        }
282        self.inner.set(tonic_request).await?;
283        Ok(())
284    }
285
286    /// Calls `gestalt.provider.v1.Cache.SetMany`.
287    pub async fn set_many(
288        &mut self,
289        entries: Vec<CacheSetEntry>,
290        ttl: Option<std::time::Duration>,
291    ) -> Result<(), GestaltError> {
292        let request = CacheSetManyRequest { entries, ttl };
293        let mut tonic_request = tonic::Request::new(to_wire_cache_set_many_request(request));
294        if let Some(timeout) = self.timeout {
295            tonic_request.set_timeout(timeout);
296        }
297        self.inner.set_many(tonic_request).await?;
298        Ok(())
299    }
300
301    /// Calls `gestalt.provider.v1.Cache.SetMany` with the full request and response messages.
302    pub async fn set_many_raw(&mut self, request: CacheSetManyRequest) -> Result<(), GestaltError> {
303        let mut tonic_request = tonic::Request::new(to_wire_cache_set_many_request(request));
304        if let Some(timeout) = self.timeout {
305            tonic_request.set_timeout(timeout);
306        }
307        self.inner.set_many(tonic_request).await?;
308        Ok(())
309    }
310
311    /// Calls `gestalt.provider.v1.Cache.Delete`.
312    pub async fn delete(&mut self, key: String) -> Result<bool, GestaltError> {
313        let request = CacheDeleteRequest { key };
314        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_request(request));
315        if let Some(timeout) = self.timeout {
316            tonic_request.set_timeout(timeout);
317        }
318        let response =
319            from_wire_cache_delete_response(self.inner.delete(tonic_request).await?.into_inner());
320        Ok(response.deleted)
321    }
322
323    /// Calls `gestalt.provider.v1.Cache.Delete` with the full request and response messages.
324    pub async fn delete_raw(
325        &mut self,
326        request: CacheDeleteRequest,
327    ) -> Result<CacheDeleteResponse, GestaltError> {
328        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_request(request));
329        if let Some(timeout) = self.timeout {
330            tonic_request.set_timeout(timeout);
331        }
332        let response = self.inner.delete(tonic_request).await?;
333        Ok(from_wire_cache_delete_response(response.into_inner()))
334    }
335
336    /// Calls `gestalt.provider.v1.Cache.DeleteMany`.
337    pub async fn delete_many(&mut self, keys: Vec<String>) -> Result<i64, GestaltError> {
338        let request = CacheDeleteManyRequest { keys };
339        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_many_request(request));
340        if let Some(timeout) = self.timeout {
341            tonic_request.set_timeout(timeout);
342        }
343        let response = from_wire_cache_delete_many_response(
344            self.inner.delete_many(tonic_request).await?.into_inner(),
345        );
346        Ok(response.deleted)
347    }
348
349    /// Calls `gestalt.provider.v1.Cache.DeleteMany` with the full request and response messages.
350    pub async fn delete_many_raw(
351        &mut self,
352        request: CacheDeleteManyRequest,
353    ) -> Result<CacheDeleteManyResponse, GestaltError> {
354        let mut tonic_request = tonic::Request::new(to_wire_cache_delete_many_request(request));
355        if let Some(timeout) = self.timeout {
356            tonic_request.set_timeout(timeout);
357        }
358        let response = self.inner.delete_many(tonic_request).await?;
359        Ok(from_wire_cache_delete_many_response(response.into_inner()))
360    }
361
362    /// Calls `gestalt.provider.v1.Cache.Touch`.
363    pub async fn touch(
364        &mut self,
365        key: String,
366        ttl: Option<std::time::Duration>,
367    ) -> Result<bool, GestaltError> {
368        let request = CacheTouchRequest { key, ttl };
369        let mut tonic_request = tonic::Request::new(to_wire_cache_touch_request(request));
370        if let Some(timeout) = self.timeout {
371            tonic_request.set_timeout(timeout);
372        }
373        let response =
374            from_wire_cache_touch_response(self.inner.touch(tonic_request).await?.into_inner());
375        Ok(response.touched)
376    }
377
378    /// Calls `gestalt.provider.v1.Cache.Touch` with the full request and response messages.
379    pub async fn touch_raw(
380        &mut self,
381        request: CacheTouchRequest,
382    ) -> Result<CacheTouchResponse, GestaltError> {
383        let mut tonic_request = tonic::Request::new(to_wire_cache_touch_request(request));
384        if let Some(timeout) = self.timeout {
385            tonic_request.set_timeout(timeout);
386        }
387        let response = self.inner.touch(tonic_request).await?;
388        Ok(from_wire_cache_touch_response(response.into_inner()))
389    }
390}