Giter Club home page Giter Club logo

genericmemorycache's Introduction

System.Runtime.Caching.Generic

Overview

This is a lightweight, strongly-typed, generic, extensible, and thread-safe, N-way set-associative, in-process (memory) cache, coming with 4 built-in eviction / replacement policies (LRU, MRU, LFU, none).

The TestApp console application accompanying the library comes with a few (NUnit) tests wrt. correctness, robustness, and extensibility, along with a basic comparative performance / stress test vs. Microsoft's System.Runtime.Caching introduced in .NET 4.0.

Please read and accept the terms of the LICENSE, or else, do not use this library as-is.

Sample use

    [Test]
    // LFU: Least Frequently Used
    public void Test_BasicLFUCacheSemantic()
    {
        var lfuCache = new MemoryCache<int, string>();
        lfuCache.SetPolicy(typeof(LfuEvictionPolicy<,>));
        
        // Note the default number of ways is 1, and the default capacity is 16...
        Assert.AreEqual(lfuCache.Capacity, AbstractCache.DefaultCapacity);

        var data =
            Enumerable.Range(0, AbstractCache.DefaultCapacity + 1).
            Aggregate(new StringBuilder(), (sb, i) => sb.Append(i > 0 ? String.Format(",String {0}", i) : String.Empty)).
            ToString().
            Split(',');

        // Cache all the (16) non-empty strings
        for (var i = 1; i < data.Length; i++)
        {
            lfuCache.Add(i, data[i]);
        }

        // Use all the (16) non-empty strings four times...
        for (var use = 1; use <= 4; use++)
        {
            for (var i = 1; i < data.Length; i++)
            {
                // ... except for "String 3", used only twice...
                if (i == 3)
                {
                    if (use <= 2)
                    {
                        var s = lfuCache.Get(i);
                    }
                }
                // ... and for "String 9", used only once
                else if (i == 9)
                {
                    if (use <= 1)
                    {
                        var s = lfuCache.Get(i);
                    }
                }
                else
                {
                    var s = lfuCache.Get(i);
                }
            }
        }

        lfuCache.Add(17, "String 17");
        Assert.AreEqual(lfuCache.Contains(9), false);
        Assert.AreEqual(lfuCache.Contains(17), true);

        var used4times = lfuCache.Get(17);
        used4times = lfuCache.Get(17);
        used4times = lfuCache.Get(17);
        used4times = lfuCache.Get(17);

        lfuCache.Put(18, "String 18");
        Assert.AreEqual(lfuCache.Contains(3), false);
        Assert.AreEqual(lfuCache.Contains(17), true);
        Assert.AreEqual(lfuCache.Contains(18), true);
    }

Object model

System.Runtime.Caching.Generic

genericmemorycache's People

Contributors

ysharplanguage avatar

Watchers

shaun arundell avatar

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.