Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InMemoryCacheClient can not get as object #133

Open
SkyvenXiong opened this issue Sep 21, 2018 · 7 comments
Open

InMemoryCacheClient can not get as object #133

SkyvenXiong opened this issue Sep 21, 2018 · 7 comments
Labels

Comments

@SkyvenXiong
Copy link

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

@niemyjski
Copy link
Member

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?

@SkyvenXiong
Copy link
Author

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

@SkyvenXiong
Copy link
Author

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

@SkyvenXiong
Copy link
Author

SkyvenXiong commented Oct 17, 2018

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

@niemyjski
Copy link
Member

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.

@SkyvenXiong
Copy link
Author

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

@ejsmith
Copy link
Contributor

ejsmith commented Oct 24, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants