gpt4 book ai didi

c# - 如何在不序列化的情况下 Redis 缓存大型 C# 对象?

转载 作者:可可西里 更新时间:2023-11-01 11:12:27 25 4
gpt4 key购买 nike

在我的应用程序中,我们使用 C# 字典在内存中缓存一个大型业务对象。

我正在将巨大的多轮引用 C# 对象(JSON 序列化时约为 300KB)转换为 DTO,以便为 Redis 缓存进行 JSON 序列化/反序列化。

由于参数化的构造函数,我的业务层中的某些类型似乎无法轻松初始化。

有没有一种方法可以让 Redis 缓存而无需序列化?这样可以返回我缓存的同一个对象吗?

最佳答案

Is there a way I can Redis cache without serialization?

简短的回答:

稍微长一点的回答:不是,因为Redis是基于字符串的。在那些字符串中,你可以存储各种各样的东西,但最终都归结为字符串。所以当你想在 Redis 中存储一些东西时,你无法摆脱序列化。

Redis is not a plain key-value store, it is actually a data structures server, supporting different kinds of values. What this means is that, while in traditional key-value stores you associated string keys to string values, in Redis the value is not limited to a simple string, but can also hold more complex data structures. The following is the list of all the data structures supported by Redis, which will be covered separately in this tutorial:

  • Binary-safe strings.
  • Lists: collections of string elements sorted according to the order of insertion. They are basically linked lists.
  • Sets: collections of unique, unsorted string elements.
  • Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).
  • Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. This is very similar to Ruby or Python hashes.
  • Bit arrays (or simply bitmaps): it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.
  • HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don't be scared, it is simpler than it seems... See later in the HyperLogLog section of this tutorial.

更多信息:An introduction to Redis data types and abstractions

关于c# - 如何在不序列化的情况下 Redis 缓存大型 C# 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53987575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com