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

[BUG] set cache, app restart, L1 Cache invalid. get data from L2 Cache, Error. #386

Open
gmij opened this issue Feb 10, 2025 · 1 comment

Comments

@gmij
Copy link

gmij commented Feb 10, 2025

Describe the bug

when app restart, L1 Cache(memory Cache) invalid, get data from L2 Cache, throw this bug .

[09:52:24 ERR] An unhandled exception has occurred while executing the request.
System.InvalidCastException: Unable to cast object of type 'System.Text.Json.JsonElement' to type 'xx.Ai.AudioMergeInfo'.
at ZiggyCreatures.Caching.Fusion.Internals.Memory.FusionCacheMemoryEntry`1.GetValueTValue1 in //src/ZiggyCreatures.FusionCache/Internals/Memory/FusionCacheMemoryEntry.cs:line 68
at ZiggyCreatures.Caching.Fusion.FusionCache.GetValueFromMemoryEntry[TValue](String operationId, String key, IFusionCacheMemoryEntry entry, FusionCacheEntryOptions options) in /
/src/ZiggyCreatures.FusionCache/FusionCache.cs:line 1000
at ZiggyCreatures.Caching.Fusion.FusionCache.TryGet[TValue](String key, FusionCacheEntryOptions options, CancellationToken token) in /_/src/ZiggyCreatures.FusionCache/FusionCache_Sync.cs:line 602
at xx.Ai.Cache.HybridCache.TryGet[T](String key, T& value) in E:\Work\xx\xx\xx.Ai\xx.Ai.Common\Cache\HybridCache.cs:line 66

this bug on
ZiggyCreatures.FusionCache.Serialization.SystemTextJson, throw System.Text.Json.JsonElement to TValue error
and
ZiggyCreatures.FusionCache.Serialization.NewtonsoftJson, throw Newtonsoft.Json.Linq.JObject to TValue error

version : v2.1.0

To Reproduce

Here's a MRE (Minimal Reproducible Example) of the issue:

  • code sample block
  • or gist
  • or a repo
  • etc

Expected behavior

Deserialize the JsonData to TValue.

Versions

I've encountered this issue on:

  • FusionCache version: v2.1.0
  • .NET version: net 8
  • OS version: win 10
  • others (eg: if applicable, the Redis/Memcached/etc version and if onprem/cloud/etc),

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.

gmij added a commit to gmij/FusionCache that referenced this issue Feb 10, 2025
Related to ZiggyCreatures#386

Fix deserialization issue when retrieving data from L2 Cache after an app restart.

* **FusionCacheMemoryEntry.cs**
  - Update `GetValue<TValue1>` method to handle deserialization correctly for `JsonElement` and `JToken`.
  - Add checks to determine if the value is of type `JsonElement` or `JToken`.
  - Use the appropriate deserializer to convert `JsonElement` or `JToken` to `TValue1`.

* **FusionCacheNewtonsoftJsonSerializer.cs**
  - Update `Deserialize<T>` method to handle conversion correctly for `JToken`.
  - Add a check to determine if the data is of type `JToken`.
  - Use the appropriate deserializer to convert `JToken` to `T`.

* **FusionCacheSystemTextJsonSerializer.cs**
  - Update `Deserialize<T>` method to handle conversion correctly for `JsonElement`.
  - Add a check to determine if the data is of type `JsonElement`.
  - Use the appropriate deserializer to convert `JsonElement` to `T`.

* **SerializationTests.cs**
  - Add tests to ensure deserialization works correctly after an app restart.
  - Create a test case for `FusionCacheNewtonsoftJsonSerializer`.
  - Create a test case for `FusionCacheSystemTextJsonSerializer`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ZiggyCreatures/FusionCache/issues/386?shareId=XXXX-XXXX-XXXX-XXXX).
@jodydonetti
Copy link
Collaborator

Hi @gmij , by a first look it seems like you have saved the value with a certain type and are trying to get it with a different type, but I can't understand more with the info provided.

Can you please provide an MRE, maybe via a GIST or similar?

Thanks!

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