Foundatio icon indicating copy to clipboard operation
Foundatio copied to clipboard

InMemoryCacheClient can not get as object

Open SkyvenXiong opened this issue 7 years ago • 7 comments
trafficstars

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

SkyvenXiong avatar Sep 21 '18 09:09 SkyvenXiong

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?

niemyjski avatar Sep 21 '18 12:09 niemyjski

I want set a T,but get as object,but it not work well.

SkyvenXiong avatar Oct 17 '18 01:10 SkyvenXiong

you can copy the code and run this case .it is failed

SkyvenXiong avatar Oct 17 '18 01:10 SkyvenXiong

it is caused by you the reason marked in picture below 486f185c-6365-4d7f-aa94-6b4589a18c3f

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

SkyvenXiong avatar Oct 17 '18 01:10 SkyvenXiong

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.

niemyjski avatar Oct 20 '18 13:10 niemyjski

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

SkyvenXiong avatar Oct 24 '18 11:10 SkyvenXiong

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.

ejsmith avatar Oct 24 '18 15:10 ejsmith