Giter Site home page Giter Site logo

Comments (4)

alastairtree avatar alastairtree commented on May 30, 2024

We could embed the type into the key but that would break the capability to cache sync and retrieve it from the async method feature which is quite convenient in a sync/async environment, so inclined not to and make the developer choose the right key. The behaviour you describe does help the developer to catch situations where they have missused the same key.

Either when you cache a new thing of different type it should:
1 - returns null as now, (will probably become a null ref runtime exception in users code)
2 - Throw an exception from inside lazycache
3 - override the thing in the cache with the new thing of different type (no exception - assumes the developer knew what they were doing - last in wins)

My preference would be option 3?

from lazycache.

avanish-fullstack avatar avanish-fullstack commented on May 30, 2024

Would agree with #3 , @alastairtree , giving the power in the hands of the developer to decide what the intention was rather than throwing an exception

from lazycache.

NeilMacMullen avatar NeilMacMullen commented on May 30, 2024

Thanks for looking at this. It it's helpful I can describe my use-case....

I have an IoT application where multiple record types are associated with each sensor. The sensor Id is the most useful key to use to index these records and it's therefore a convenience to store everything associated with a particular sensor in a single cache. So for sensor "1234" the cache might contain a location-record and a power-record. All the client libraries I'm using are using generics and so storing/fetching by key+type is fairly natural. As it happened, I just wrote some extension methods to get the behaviour I wanted....

/// <summary>
    /// Provides wrappers around LazyCache that allow the same key to be used for different object types
    /// </summary>
    public static class CacheExtension
    {
        private static string MakeKey<T>(string key)
        {
            return $"{typeof(T)}" + key;
        }
        public static void SafeAdd<T>(this IAppCache cache,string key,T val)
        {
            cache.Add(MakeKey<T>(key),val); 
        }

        public static Task<T> SafeGetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> fetcher)
        {
            return cache.GetOrAddAsync(MakeKey<T>(key), fetcher);
        }

        public static T SafeGetOrAdd<T>(this IAppCache cache, string key, Func<T> fetcher)
        {
            return cache.GetOrAdd(MakeKey<T>(key), fetcher);
        }
    } 

While I'm commenting.... you probably have good design reasons for doing it this way but it would be a nicety (at least for the sync version) to have a GetOrAdd prototype that takes a Func<string,T> so that the nicer method-group syntax could be used....

var foo = cache.GetOrAdd("key", FetchFoo);
Foo FetchFoo(string key) {..}

from lazycache.

alastairtree avatar alastairtree commented on May 30, 2024

This will be fixed in 2.0.3 so that last in wins and if you cache 2 types with the same key, the second type will force the eviction of the first. However unless you have a really good reason, always use different keys for different types.

Thanks

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.