Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_through_cache does not cache data for list/select by query operations #133

Open
blono opened this issue Oct 7, 2023 · 4 comments
Open
Labels
enhancement New feature or request

Comments

@blono
Copy link

blono commented Oct 7, 2023

I wrote the following code to test if it can be cached with read_through_cache and read from cache with read_cached_only.

pub async fn init_db_cache(db: &FirestoreDb, cache_name: &str, collection_path: &str, parent_path: ParentPathBuilder, listener_target: u32, max_capacity: u64) -> Result<FirestoreCache<FirestoreMemoryCacheBackend, FirestoreMemListenStateStorage>, FirestoreError> {
    let conf = FirestoreCacheCollectionConfiguration::new(
        collection_path,
        FirestoreListenerTarget::new(listener_target),
        FirestoreCacheCollectionLoadMode::PreloadNone,
    ).with_parent(parent_path);

    let mut cache = FirestoreCache::new(
        cache_name.into(),
        db,
        FirestoreMemoryCacheBackend::with_max_capacity(
            FirestoreCacheConfiguration::new().add_collection_config(&db, conf),
            max_capacity
        )?,
        FirestoreMemListenStateStorage::new(),
    ).await?;

    cache.load().await?;

    Ok(cache)
}

async fn main() -> Result<()> {
    let db = FirestoreDb::new(&config_env_var("PROJECT_ID")?).await?;
    let parent_path = db.parent_path("p", "c").unwrap();
    let cache = init_db_cache(&db, "mem-cache", "mydoc", parent_path, 1000, 32 * 1024 * 1024);

    let _ = db.read_through_cache(&cache)
        .fluent()
        .list()
        .from("mydoc")
        .parent(&parent_path)
        .order_by([
            (path!(MyDoc::last_update_millis), FirestoreQueryDirection::Descending),
        ])
        .obj()
        .stream_all_with_errors()
        .await?
        .try_collect::<Vec<MyDoc>>()
        .await;

    let list = db.read_cached_only(&cache) // Should be able to get list.
        .fluent()
        .list()
        .from("mydoc")
        .parent(&parent_path)
        .order_by([
            (path!(MyDoc::last_update_millis), FirestoreQueryDirection::Descending),
        ])
        .obj()
        .stream_all_with_errors()
        .await?
        .try_collect::<Vec<MyDoc>>()
        .await;
}

I expected to be able to retrieve the list in the subsequent read_cached_only because it is cached in read_through_cache, but I could not retrieve it.

I also tested PreloadAllDocs with a different code, and it worked fine!

@abdolence
Copy link
Owner

abdolence commented Oct 7, 2023

Hey,

This is not related to parent per see, more like the cache update supports only 3 sources right now:

Described here:
https://github.com/abdolence/firestore-rs#how-a-cache-is-updated

  • Reading using get by IDs
  • Listener updates
  • Preload

So, the results from list actually is not cached at least right now, mostly because they might be incomplete, etc. This is not implemented at least yet.
I'll think about it.

PS. Also, I recommend avoid using list for big collections, since for some reason Google doesn't like the idea with big page sizes, and this operation is slower than just select.

@abdolence
Copy link
Owner

Currently I recommend using PreloadAll if you want to work with querying/listing ready-to-use caches.

@abdolence abdolence added the enhancement New feature or request label Oct 7, 2023
@abdolence abdolence changed the title read_through_cache does not cache when parent is specified read_through_cache does not cache data for list/query operations Oct 7, 2023
@abdolence abdolence changed the title read_through_cache does not cache data for list/query operations read_through_cache does not cache data for list/select operations Oct 7, 2023
@abdolence abdolence changed the title read_through_cache does not cache data for list/select operations read_through_cache does not cache data for list/select by query operations Oct 7, 2023
@blono
Copy link
Author

blono commented Oct 7, 2023

I see and understand!
I guess I was mistaken!

@blono blono closed this as completed Oct 7, 2023
@abdolence
Copy link
Owner

abdolence commented Oct 7, 2023

Let's leave the issue opened, to make it more visible and remind me about this limitation :)

@abdolence abdolence reopened this Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants