Spaces:
Runtime error
Runtime error
Spencerjibz
commited on
Commit
•
f5cf5f9
1
Parent(s):
c762f9c
changed cache_results method to support multiple values
Browse files- src/cache/cacher.rs +44 -22
src/cache/cacher.rs
CHANGED
@@ -4,6 +4,7 @@
|
|
4 |
use error_stack::Report;
|
5 |
#[cfg(feature = "memory-cache")]
|
6 |
use mini_moka::sync::Cache as MokaCache;
|
|
|
7 |
|
8 |
#[cfg(feature = "memory-cache")]
|
9 |
use std::time::Duration;
|
@@ -61,8 +62,8 @@ pub trait Cacher: Send + Sync {
|
|
61 |
/// failure.
|
62 |
async fn cache_results(
|
63 |
&mut self,
|
64 |
-
search_results: &SearchResults,
|
65 |
-
|
66 |
) -> Result<(), Report<CacheError>>;
|
67 |
|
68 |
/// A helper function which computes the hash of the url and formats and returns it as string.
|
@@ -332,14 +333,29 @@ impl Cacher for RedisCache {
|
|
332 |
|
333 |
async fn cache_results(
|
334 |
&mut self,
|
335 |
-
search_results: &SearchResults,
|
336 |
-
|
337 |
) -> Result<(), Report<CacheError>> {
|
338 |
use base64::Engine;
|
339 |
-
let bytes =
|
340 |
-
|
341 |
-
|
342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
}
|
344 |
}
|
345 |
/// TryInto implementation for SearchResults from Vec<u8>
|
@@ -391,12 +407,16 @@ impl Cacher for InMemoryCache {
|
|
391 |
|
392 |
async fn cache_results(
|
393 |
&mut self,
|
394 |
-
search_results: &SearchResults,
|
395 |
-
|
396 |
) -> Result<(), Report<CacheError>> {
|
397 |
-
|
398 |
-
|
399 |
-
|
|
|
|
|
|
|
|
|
400 |
Ok(())
|
401 |
}
|
402 |
}
|
@@ -434,11 +454,13 @@ impl Cacher for HybridCache {
|
|
434 |
|
435 |
async fn cache_results(
|
436 |
&mut self,
|
437 |
-
search_results: &SearchResults,
|
438 |
-
|
439 |
) -> Result<(), Report<CacheError>> {
|
440 |
-
self.redis_cache.cache_results(search_results,
|
441 |
-
self.memory_cache
|
|
|
|
|
442 |
|
443 |
Ok(())
|
444 |
}
|
@@ -460,8 +482,8 @@ impl Cacher for DisabledCache {
|
|
460 |
|
461 |
async fn cache_results(
|
462 |
&mut self,
|
463 |
-
_search_results: &SearchResults,
|
464 |
-
|
465 |
) -> Result<(), Report<CacheError>> {
|
466 |
Ok(())
|
467 |
}
|
@@ -519,11 +541,11 @@ impl SharedCache {
|
|
519 |
/// on a failure.
|
520 |
pub async fn cache_results(
|
521 |
&self,
|
522 |
-
search_results: &SearchResults,
|
523 |
-
|
524 |
) -> Result<(), Report<CacheError>> {
|
525 |
let mut mut_cache = self.cache.lock().await;
|
526 |
-
mut_cache.cache_results(search_results,
|
527 |
}
|
528 |
}
|
529 |
|
|
|
4 |
use error_stack::Report;
|
5 |
#[cfg(feature = "memory-cache")]
|
6 |
use mini_moka::sync::Cache as MokaCache;
|
7 |
+
use mini_moka::sync::ConcurrentCacheExt;
|
8 |
|
9 |
#[cfg(feature = "memory-cache")]
|
10 |
use std::time::Duration;
|
|
|
62 |
/// failure.
|
63 |
async fn cache_results(
|
64 |
&mut self,
|
65 |
+
search_results: &[SearchResults],
|
66 |
+
urls: &[String],
|
67 |
) -> Result<(), Report<CacheError>>;
|
68 |
|
69 |
/// A helper function which computes the hash of the url and formats and returns it as string.
|
|
|
333 |
|
334 |
async fn cache_results(
|
335 |
&mut self,
|
336 |
+
search_results: &[SearchResults],
|
337 |
+
urls: &[String],
|
338 |
) -> Result<(), Report<CacheError>> {
|
339 |
use base64::Engine;
|
340 |
+
let mut bytes = Vec::with_capacity(3);
|
341 |
+
|
342 |
+
for result in search_results {
|
343 |
+
let processed = self.pre_process_search_results(result)?;
|
344 |
+
bytes.push(processed);
|
345 |
+
}
|
346 |
+
|
347 |
+
let base64_strings = bytes
|
348 |
+
.iter()
|
349 |
+
.map(|bytes_vec| base64::engine::general_purpose::STANDARD_NO_PAD.encode(bytes_vec));
|
350 |
+
|
351 |
+
let mut hashed_url_strings = Vec::with_capacity(3);
|
352 |
+
|
353 |
+
for url in urls {
|
354 |
+
let hash = self.hash_url(url);
|
355 |
+
hashed_url_strings.push(hash);
|
356 |
+
}
|
357 |
+
self.cache_json(base64_strings, hashed_url_strings.into_iter())
|
358 |
+
.await
|
359 |
}
|
360 |
}
|
361 |
/// TryInto implementation for SearchResults from Vec<u8>
|
|
|
407 |
|
408 |
async fn cache_results(
|
409 |
&mut self,
|
410 |
+
search_results: &[SearchResults],
|
411 |
+
urls: &[String],
|
412 |
) -> Result<(), Report<CacheError>> {
|
413 |
+
for (url, search_result) in urls.iter().zip(search_results.iter()) {
|
414 |
+
let hashed_url_string = self.hash_url(url);
|
415 |
+
let bytes = self.pre_process_search_results(search_result)?;
|
416 |
+
self.cache.insert(hashed_url_string, bytes);
|
417 |
+
}
|
418 |
+
|
419 |
+
self.cache.sync();
|
420 |
Ok(())
|
421 |
}
|
422 |
}
|
|
|
454 |
|
455 |
async fn cache_results(
|
456 |
&mut self,
|
457 |
+
search_results: &[SearchResults],
|
458 |
+
urls: &[String],
|
459 |
) -> Result<(), Report<CacheError>> {
|
460 |
+
self.redis_cache.cache_results(search_results, urls).await?;
|
461 |
+
self.memory_cache
|
462 |
+
.cache_results(search_results, urls)
|
463 |
+
.await?;
|
464 |
|
465 |
Ok(())
|
466 |
}
|
|
|
482 |
|
483 |
async fn cache_results(
|
484 |
&mut self,
|
485 |
+
_search_results: &[SearchResults],
|
486 |
+
_urls: &[String],
|
487 |
) -> Result<(), Report<CacheError>> {
|
488 |
Ok(())
|
489 |
}
|
|
|
541 |
/// on a failure.
|
542 |
pub async fn cache_results(
|
543 |
&self,
|
544 |
+
search_results: &[SearchResults],
|
545 |
+
urls: &[String],
|
546 |
) -> Result<(), Report<CacheError>> {
|
547 |
let mut mut_cache = self.cache.lock().await;
|
548 |
+
mut_cache.cache_results(search_results, urls).await
|
549 |
}
|
550 |
}
|
551 |
|