neon_arch commited on
Commit
4bb6c5e
·
1 Parent(s): 494ff27

:zap: perf: initialize vectors with capacity by default & use `Arc<T>` to partially clone memory cache struct (#603)

Browse files
Files changed (1) hide show
  1. src/cache/cacher.rs +28 -13
src/cache/cacher.rs CHANGED
@@ -2,10 +2,9 @@
2
  //! from the upstream search engines in a json format.
3
 
4
  use error_stack::Report;
 
5
  #[cfg(feature = "memory-cache")]
6
- use mini_moka::sync::Cache as MokaCache;
7
- #[cfg(feature = "memory-cache")]
8
- use mini_moka::sync::ConcurrentCacheExt;
9
 
10
  #[cfg(feature = "memory-cache")]
11
  use std::time::Duration;
@@ -376,13 +375,13 @@ impl Cacher for RedisCache {
376
  }
377
  }
378
  /// TryInto implementation for SearchResults from Vec<u8>
379
- use std::convert::TryInto;
380
 
381
  impl TryInto<SearchResults> for Vec<u8> {
382
  type Error = CacheError;
383
 
384
  fn try_into(self) -> Result<SearchResults, Self::Error> {
385
- serde_json::from_slice(&self).map_err(|_| CacheError::SerializationError)
386
  }
387
  }
388
 
@@ -390,7 +389,7 @@ impl TryInto<Vec<u8>> for &SearchResults {
390
  type Error = CacheError;
391
 
392
  fn try_into(self) -> Result<Vec<u8>, Self::Error> {
393
- serde_json::to_vec(self).map_err(|_| CacheError::SerializationError)
394
  }
395
  }
396
 
@@ -398,7 +397,16 @@ impl TryInto<Vec<u8>> for &SearchResults {
398
  #[cfg(feature = "memory-cache")]
399
  pub struct InMemoryCache {
400
  /// The backend cache which stores data.
401
- cache: MokaCache<String, Vec<u8>>,
 
 
 
 
 
 
 
 
 
402
  }
403
 
404
  #[cfg(feature = "memory-cache")]
@@ -408,15 +416,17 @@ impl Cacher for InMemoryCache {
408
  log::info!("Initialising in-memory cache");
409
 
410
  InMemoryCache {
411
- cache: MokaCache::builder()
412
- .time_to_live(Duration::from_secs(config.cache_expiry_time.into()))
413
- .build(),
 
 
414
  }
415
  }
416
 
417
  async fn cached_results(&mut self, url: &str) -> Result<SearchResults, Report<CacheError>> {
418
  let hashed_url_string = self.hash_url(url);
419
- match self.cache.get(&hashed_url_string) {
420
  Some(res) => self.post_process_search_results(res).await,
421
  None => Err(Report::new(CacheError::MissingValue)),
422
  }
@@ -427,13 +437,18 @@ impl Cacher for InMemoryCache {
427
  search_results: &[SearchResults],
428
  urls: &[String],
429
  ) -> Result<(), Report<CacheError>> {
 
430
  for (url, search_result) in urls.iter().zip(search_results.iter()) {
431
  let hashed_url_string = self.hash_url(url);
432
  let bytes = self.pre_process_search_results(search_result).await?;
433
- self.cache.insert(hashed_url_string, bytes);
 
 
 
434
  }
435
 
436
- self.cache.sync();
 
437
  Ok(())
438
  }
439
  }
 
2
  //! from the upstream search engines in a json format.
3
 
4
  use error_stack::Report;
5
+ use futures::future::join_all;
6
  #[cfg(feature = "memory-cache")]
7
+ use moka::future::Cache as MokaCache;
 
 
8
 
9
  #[cfg(feature = "memory-cache")]
10
  use std::time::Duration;
 
375
  }
376
  }
377
  /// TryInto implementation for SearchResults from Vec<u8>
378
+ use std::{convert::TryInto, sync::Arc};
379
 
380
  impl TryInto<SearchResults> for Vec<u8> {
381
  type Error = CacheError;
382
 
383
  fn try_into(self) -> Result<SearchResults, Self::Error> {
384
+ bincode::deserialize_from(self.as_slice()).map_err(|_| CacheError::SerializationError)
385
  }
386
  }
387
 
 
389
  type Error = CacheError;
390
 
391
  fn try_into(self) -> Result<Vec<u8>, Self::Error> {
392
+ bincode::serialize(self).map_err(|_| CacheError::SerializationError)
393
  }
394
  }
395
 
 
397
  #[cfg(feature = "memory-cache")]
398
  pub struct InMemoryCache {
399
  /// The backend cache which stores data.
400
+ cache: Arc<MokaCache<String, Vec<u8>>>,
401
+ }
402
+
403
+ #[cfg(feature = "memory-cache")]
404
+ impl Clone for InMemoryCache {
405
+ fn clone(&self) -> Self {
406
+ Self {
407
+ cache: self.cache.clone(),
408
+ }
409
+ }
410
  }
411
 
412
  #[cfg(feature = "memory-cache")]
 
416
  log::info!("Initialising in-memory cache");
417
 
418
  InMemoryCache {
419
+ cache: Arc::new(
420
+ MokaCache::builder()
421
+ .time_to_live(Duration::from_secs(config.cache_expiry_time.into()))
422
+ .build(),
423
+ ),
424
  }
425
  }
426
 
427
  async fn cached_results(&mut self, url: &str) -> Result<SearchResults, Report<CacheError>> {
428
  let hashed_url_string = self.hash_url(url);
429
+ match self.cache.get(&hashed_url_string).await {
430
  Some(res) => self.post_process_search_results(res).await,
431
  None => Err(Report::new(CacheError::MissingValue)),
432
  }
 
437
  search_results: &[SearchResults],
438
  urls: &[String],
439
  ) -> Result<(), Report<CacheError>> {
440
+ let mut tasks: Vec<_> = Vec::with_capacity(urls.len());
441
  for (url, search_result) in urls.iter().zip(search_results.iter()) {
442
  let hashed_url_string = self.hash_url(url);
443
  let bytes = self.pre_process_search_results(search_result).await?;
444
+ let new_self = self.clone();
445
+ tasks.push(tokio::spawn(async move {
446
+ new_self.cache.insert(hashed_url_string, bytes).await
447
+ }));
448
  }
449
 
450
+ join_all(tasks).await;
451
+
452
  Ok(())
453
  }
454
  }