Giter Site home page Giter Site logo

plamber / synchronizedcache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from briandunnington/synchronizedcache

0.0 0.0 0.0 11 KB

.NET in-memory cache with distributed invalidation using Azure Service Bus

License: MIT License

C# 98.63% HTML 1.37%

synchronizedcache's Introduction

SynchronizedCache Example Project

This project demonstrates how to use the SynchronizedCache class to implement an in-memory cache that supports distributed invalidation.

Azure Setup

Create a Service Bus resource, and then create a new Topic named 'synchronizedcache'. Copy the default primary connection string.

Project Setup

Clone the repository. Enter you Service Bus connection string in Startup.cs.

Place a breakpoint in the ProcessMessageAsync() method of SynchronizedCache.cs.

Testing the Cache

To increase the 'woah' factor, it is best to run the app on multiple computers at once. It will be fine if you don't, but it would be cool if you did.

This project contains an example of two caches - one that returns types of animals, and one that returns types of cars. (It is admittedly very simple, contrived, and a bit pointless, but imagine that you are going to cache your super awesome objects instead if that helps).

Run the project and then browse to /api/animal/dog - this will load the Dog animal. Since it is the first run, it will not be cached and will be loaded from source (with an artificial 5 second delay to represent an expensive DB call or whatever).

Now refresh the page to load the Dog animal again - this time it will load from the cache and be instant.

Now browse to /api/animal/cat to load another item into the cache. Refresh the page to confirm that it loads immediately.

Now browse to /api/animal/dog/flush to force the Dog item to be removed from the cache. (Again, you can imagine a real business process that caused the item to change and need to be invalidated).

You should see your breakpoint get hit. When the item was invalidated from the cache, a message was sent to the service bus topic, which sent a copy to each subscriber. (If you had the app running on multiple machines, all of them should have been notified).

The CarCache is almost exactly the same, but demonstrates using an arbitrary object for the cache key. Cache retrievals and invalidations are done using a CarType object. Whenever the cache is accessed, the GetHashKey() function is called and lets you construct a composite key from the object.

You can use the following urls to test it out:

  • api/car/ford/focus
  • api/car/tesla/modelX
  • api/car/jeep/wrangler

Then you can append /flush to any of them to remove them from the cache. `

synchronizedcache's People

Contributors

briandunnington 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.