Giter Site home page Giter Site logo

Generic Type Keys about lazycache HOT 5 CLOSED

alastairtree avatar alastairtree commented on May 30, 2024
Generic Type Keys

from lazycache.

Comments (5)

alastairtree avatar alastairtree commented on May 30, 2024

Can you give an example of what you mean as not sure I follow? What sort of syntax would you like? If you just wanted to use the cached item type as a key you could easily write an extension method that used the types of the existing generic parameter to generate a string based on namespace and type name for example.

from lazycache.

mansoor945 avatar mansoor945 commented on May 30, 2024

Right now I am using Concurrent Dictionary in which the key is GenericType:
ConcurrentDictionary<RequestIdentity, Action<IDbCommand, object>>.

I am looking for something same like this IAppCache.Add<T1, T2>(T1 key, T2 item)

from lazycache.

alastairtree avatar alastairtree commented on May 30, 2024

The problem with adding this to LazyCache is it means that you cannot cache 2 objects of the same type, as the second one would replace the first. This might lead to unexpected issues for an inexperienced user. Why not just add an extension method in your project that converted from type to string key by reflecting on the name, something like var key = typeof(T1).FullName;. Some extension methods for inspiration are in https://github.com/alastairtree/LazyCache/blob/feat/netcore2/LazyCache/AppCacheExtenions.cs

from lazycache.

Bilal-Nectari avatar Bilal-Nectari commented on May 30, 2024

@alastairtree: I can understand the problem that we can have by adding that feature in the library. However, we did some custom code on our end to bypass that issue. We basically implemented a multi-layer cache that uses internally a ConcurrentDictionary of ConcurrentDictionary. The key of the first dictionary is a string and then they key of the other dictionary is generic. The problem with that solution is that it's very limited, we don't have all the features of MemoryCache, which enables the developer to manipulate the cache based on the time and memory.

It will be really great if we can enhance the LazyCache and add the requested feature in it. I know it will be hard, since LazyCache is coded over the MemoryCache which does not support generic keys. A lot of people already complained about that limitation in the past.

from lazycache.

alastairtree avatar alastairtree commented on May 30, 2024

I think you could solve this with a simple extension method like the below that uses the type name as the key? I would not build it into LazyCache as may confuse new developers but no reason not to add it into your project? You could also tweak it to use a different type param as the key like in your example.

namespace LazyCache
{
    public static class AppCacheExtenions
    {
        public static T GetOrAdd<T>(this IAppCache cache, Func<T> addItemFactory)
        {
            if (cache == null) throw new ArgumentNullException(nameof(cache));

            var key = typeof(T).FullName;

            return cache.GetOrAdd(key, addItemFactory);
        }
    }
}

from lazycache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.