I have relation between two classes Credentials<=>UserData
. And I would like to use MemoryCache
to filter my incoming request.
My key = Credential
and value = UserData
.
(Data)MemoryCache.Default.AddOrGetExisting(GetKey(credential), // must be a string
userData,
DateTime.Now.AddMinutes(5));
How can I implement public string GetKey(Credentials credential)
for incoming request?
Credentials
its a DataContract
that contains other DataContracts
like GoogleCredentials
, FacebookCredentials
. And they contains their own strings
like user_name
and password
.
Now the cache items are added with keys credential.ToString()
and it is important for this method to return the same value for Credentials
objects having the same credential values, and distinct values for Credentials
instances with different credential values.
Inside Credential class I have the following method
public override int GetHashCode()
{
int hashCode = 0;
if (GoogleCredentials!= null)
{
hashCode ^= GoogleCredentials.GetHashCode();
}
if (FacebookCredentials!= null)
{
hashCode ^= FacebookCredentials.GetHashCode();
}
return hashCode;
}