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

Support for IDistributedCache #59

Open
alexdobarganes opened this issue Mar 20, 2019 · 15 comments
Open

Support for IDistributedCache #59

alexdobarganes opened this issue Mar 20, 2019 · 15 comments

Comments

@alexdobarganes
Copy link

alexdobarganes commented Mar 20, 2019

[To support this feature request please add a thumbs up]

It would be good to extend this current project to support IDistributedCache!

@alexdobarganes
Copy link
Author

alexdobarganes commented Mar 20, 2019

I was thinking on extending te current ICacheProvider interface to something like this.

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;

namespace LazyCache.Providers
{
    public class DistributedCacheProvider: ICacheProvider
    {
        private readonly IDistributedCache _cache;

        public DistributedCacheProvider(IDistributedCache cache)
        {
            _cache = cache;
        }
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Set(string key, object item, MemoryCacheEntryOptions policy)
        {
            throw new NotImplementedException();
        }

        public object Get(string key)
        {
            throw new NotImplementedException();
        }

        public object GetOrCreate<T>(string key, Func<ICacheEntry, T> func)
        {
            throw new NotImplementedException();
        }

        public void Remove(string key)
        {
            throw new NotImplementedException();
        }

        public async Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> func)
        {
            throw new NotImplementedException();
        }
    }
}

@alastairtree
Copy link
Owner

Yeah something like that would be the way to go. The challenge is that you need to come up with a way to handle the binary serialisation required by IDistributedCache (I suggest BSON as a default, but using another provider model for the serialiser to allow anyone to swap out the binary serialiser if needed) and you need to solve the fact that MemoryCacheEntryOptions dont make sense for IDistributedCache, instead you need DistributedCacheEntryOptions

@alexdobarganes
Copy link
Author

Yes, I agree. I thik that part sholdnt be a big deal. What is stopping me currently is the fact what is being stored is the Lazy "Func<ICacheEntry, T> factory" I'm kinda confused here is what im doing! So when i run this code my result is the func not the result from the factory method!

public object GetOrCreate<T>(string key, Func<ICacheEntry, T> factory)
            {
                if (!cache.TryGetValue(key, out object result))
                {
                    //TODO i guess at this point i need to create a CacheEntry and pass it to the factory like factory(entry)
                    result = factory(null);
                    
                    //Im just trying to set some policy here
                    Set(key, result, new MemoryCacheEntryOptions
                    {
                        SlidingExpiration = TimeSpan.FromSeconds(20)
                    });
                }

                return (T)result;
            }

@alexdobarganes
Copy link
Author

alexdobarganes commented Mar 21, 2019 via email

@alastairtree
Copy link
Owner

I don't think there is any point in putting the lazy into the distributed cache, because it will be a serialised version not the original one with references to real databases and connections and stuff in the factory. Better to wait till the lazy is evaluated and cache the result in the distributed cache I think.

@alexdobarganes
Copy link
Author

Yes, I was originally inclined to do that. Probably my fatigue yesterday blind my mind!

@alexdobarganes
Copy link
Author

I think i got it! I'll do a pull request so you can take a look!

@alexdobarganes
Copy link
Author

How can I share this code with you i tried to push a new branch but not success could you create a new branch with open access?

@alastairtree
Copy link
Owner

You need to fork the repo and push to your fork, then you can open a PR from your fork branch into here

@alexdobarganes
Copy link
Author

Thanks

@alexdobarganes
Copy link
Author

Do you mind taking a look into it https://github.com/alexdobarganes/LazyCache/tree/distributed-cache

@alastairtree
Copy link
Owner

I created a PR for your work @alexdobarganes at #63

@miguelcrpinto
Copy link

miguelcrpinto commented May 13, 2020

any news on this? the last comments where done more than an year ago

@tvblomberg
Copy link

Any news on this?

@alastairtree
Copy link
Owner

No, have not had a need for it so have not put any time into it, and the PR seems to have stalled.

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

No branches or pull requests

4 participants