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

[QUESTION] Can you tell if the cache hit comes from Memory or Distributed? #194

Closed
JarrodOsborne opened this issue Feb 7, 2024 · 8 comments

Comments

@JarrodOsborne
Copy link

I have a multi-layer FusionCache set up (in-memory & Redis).

My issue is that objects retrieved from the memory store need to be immutable, so I will likely have to clone them upon retrieval.
However I don't want to clone if the hit comes from Redis as that would be pointless. Is there a way to determine if the cache item retrieved came from in-memory or distributed?

@jodydonetti
Copy link
Collaborator

Hi @JarrodOsborne and thanks for using FusionCache!

Currently there's no way to know that when getting a value, but let me ask how are you approaching the cloning itself? Maybe I can think of something, you are not the first one to think about wanting to clone objects you got back from the cache.

@jodydonetti
Copy link
Collaborator

Closing as there has not been a response.

@JarrodOsborne
Copy link
Author

JarrodOsborne commented Feb 18, 2024

Hi @jodydonetti
I ended up writing a wrapper around IMemoryCache which does a clone,

public bool TryGetValue(object key, out object value)
{
    var exists = _memoryCache.TryGetValue(key, out object innerValue);
    value = innerValue?.Clone();
    return exists;
}

and providing this version of IMemoryCache to FusionCache.

The clone is just a JSON copy

public static T Clone<T>(this T source) where T : class
{
    return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source, _settings), _settings);
}

@jodydonetti
Copy link
Collaborator

Hi @JarrodOsborne , oooh that is smart, I haven't thought about it!

Glad it worked out in the end.

@JarrodOsborne
Copy link
Author

@jodydonetti Going back to the original question, would this feature be considered in the future? I understand it's a little niche, but now I'm at the stage where I'm logging the duration of my cache operations and it would be nice to know what layer I'm hitting.
Unless there's another way to achieve this that I don't know about?

@jodydonetti
Copy link
Collaborator

Hi @JarrodOsborne , I'm not planning it right now, but I'm still thinking about how it could be made without a major change in the api surface area.

Do you have any hints or ideas you'd like to share?

@jodydonetti
Copy link
Collaborator

jodydonetti commented Aug 4, 2024

Hi all, Auto-Cloning is coming in v1.3.0 which will be out... I think later today 😬

Will update later.

@jodydonetti
Copy link
Collaborator

Hi all, v1.3.0 is out 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants