Foundatio
Foundatio copied to clipboard
InMemoryCacheClient can not get as object
this is the unit test
public class Test {
public long Id { get; set; }
public string Name { get; set; }
}
[Fact]
public async Task GetObjectTest() {
var client = new InMemoryCacheClient(o => o.LoggerFactory(Log));
await client.SetAsync("test", new Test {Id = 100, Name = "test"});
var data =await client.GetAsync<object>("test");
Assert.True(data.HasValue);
Assert.IsType(typeof(Test),data.Value);
}
in the CacheValue .GetValue method , you judge the T as object made this problem . i think you can delete this judgement. or what is your reason for that
When ever you are doing a get of a reference type, we make a complete clone of the object and return it. So when you say GetAsync<object> you are getting a clone back. We do this because otherwise any changes you make to this object that was returned would also change the cached copy. Please let me know if you have any questions or this is not what you expected.
Were you thinking you'd get a copy back as type T?
I want set a T,but get as object,but it not work well.
you can copy the code and run this case .it is failed
it is caused by you the reason marked in picture below

if you want clone the object. you can support the ICloneable interface if the object implement it. because if the object is poco do not have any reference property. the object 's root method "MemberwiseClone" will work well, so the more Extensibility design is : use the ICloneable if object implement it .otherwise use your deepclone.
about the clone it an other question. different from my original question
Would you mind doing a pr for this support as well as add the test. Currently we are using the DeepCloner library.. I wonder if there is something we can do to preserve the type.
you can copy the code i given at the first and run it in you test project. in my project ,has use you in memoryclient code and fixed this problem. only delete code in the red box in the picture
The object is serialized and deserialized. We need to know what datatype to deserialize to. We could always wrap the data in an envelope with type information embedded into it, but not sure how I feel about that. Remember that you are just looking at the in memory implementation there are implementations in Redis and others as well that need to be taken into consideration.