Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MemoryCache使用两个项作为LocalCache的键实现_C#_Asp.net_.net_Caching_Memorycache - Fatal编程技术网

C#MemoryCache使用两个项作为LocalCache的键实现

C#MemoryCache使用两个项作为LocalCache的键实现,c#,asp.net,.net,caching,memorycache,C#,Asp.net,.net,Caching,Memorycache,我在SP中使用两个输入参数(UserID、ProfileID)从数据库检索数据。我想将结果存储在memoryCache中,因为结果在一段时间内是静态的。我想用 字符串cacheKey=UserID.toString()+“”+ProfileID.toString()作为键。如果我使用concat字符串,这是一个非常简单的解决方案 我可以使用静态ConcurrentDictionary作为缓存,使用元组作为缓存键,但我确实需要在一定时间后使缓存过期 我想知道任何其他替代方法/想法,如果字符串串接用

我在SP中使用两个输入参数(UserID、ProfileID)从数据库检索数据。我想将结果存储在memoryCache中,因为结果在一段时间内是静态的。我想用

字符串cacheKey=UserID.toString()+“”+ProfileID.toString()作为键。如果我使用concat字符串,这是一个非常简单的解决方案

我可以使用静态ConcurrentDictionary作为缓存,使用元组作为缓存键,但我确实需要在一定时间后使缓存过期


我想知道任何其他替代方法/想法,如果字符串串接用作键有任何缺点。

您针对缓存键的字符串串接解决方案是好的,我将使用该解决方案

当心如果您想使用元组作为缓存键,ConcurrentDictionary接受任何对象作为缓存键,但实际使用它作为键取决于“GetHashCode()”对象方法,因此您需要验证元组生成的HashCode是否一致,否则会出现错误


字符串连接是一个更简单的解决方案。

另一个选项是扩展tuple类并重写GetHashCode方法以输出字符串连接。通过这种方式,您将拥有一个表示密钥的类,这可能会使其他使用本地缓存的开发人员更加容易。