0

I am thinking about making my own caching system, mainly for C#, but this is more a design question, so the language doesn't really matter.

Let me start at the beginning, I have a basic web api, which uses the ETag-header to check if something changed.

This is the ICachingProvider, it is the base interface for all caching providers.

public interface ICachingProvider {
    // Writes a caching entry
    void WriteCachingEntry<T>(string uri, string md5, T data);
    // Reads a caching entry sync
    Tuple<string, T> ReadCachingEntry<T>(string uri);
    // Reads a caching entry async
    Task<Tuple<string, T>> ReadCachingEntryAsync<T>(string uri);
    // Clears the cache
    void ClearCache();
    // The current storage size
    long Size { get; }
    // How much can be stored maximum
    long MaxSize { get; }
}

Currently, I have build a MemoryCahingProvider and a FileSystemCachingProvider where the last one is just storing JSON files in the isolated storage.

Now my plan is, to also implement a database caching system, I hope for better speed and less space use, maybe I am wrong with that.

To get to the point of the question, I am unsure which type of database I should use. First I thought of just a SQLlite database, but that would be not really efficient, cause I have more than one type of element, similar to the Facebook API. The next shot would be CouchDB or MongoDB, but I don't want to install more software on the client system than needed.

Does anybody have experience with that kind of cache or has someone an idea how to build something like it and with which database type?

  • 1
    You already have in-memory cache, database can't better that. If you want less space then probably compress huge items before storing to cache. – imel96 Feb 13 '15 at 6:53
  • The reason why I don't like the in memory cache is, it is cleared after closing the application, file system and database would stay :) – Knerd Feb 13 '15 at 7:43
  • 1
    Since you say this is for web api, uou could use memory outside your application, like memcached. – imel96 Feb 16 '15 at 3:57
1

You might want to look around the System.Web.Caching documentation on MSDN. There are a few good abstract ideas about Caching data that are relevant to any system including outside of a Website.

For example, the CacheDependency class:

Establishes a dependency relationship between an item stored in an ASP.NET application's Cache object and a file, cache key, an array of either, or another CacheDependency object. The CacheDependency class monitors the dependency relationships so that when any of them changes, the cached item will be automatically removed.

And although your cache sounds like it won't have anything to do with ASP.Net's cache, following this example in creating your own should be beneficial.

As for your ICachingProvider the OutputCacheProvider states:

Notes to Inheritors When you inherit from OutputCacheProvider, you must override the following members:

• Add

• Get

• Remove

• Set

I find it's good practice to see what Microsoft's created and the naming conventions they use for similiar object and then re-use those ideas for my own classes. It's beneficial because other programmers who might use your classes and have any experience with similar classes from Microsoft should have an easier time implementing your solution.

The next shot would be CouchDB or MongoDB, but I don't want to install more software on the client system than needed.

If you're building a Database Cache, it shouldn't care what the backend is (unless that's where you are storing the cache, but then I'd asked how that would be any faster then a database in general).

  • Good answer, but the library is not an ASP.net library, that is just the server. So the whole web cache stuff doesn't apply, kind of sadly :/ Yes, I want to store the cache in a database, my idea how it could be faster was that the accesstime SQL Server vs. file system are a win for the SQL Server ;) – Knerd Feb 13 '15 at 7:42
  • the library is not an ASP.net library.. yes I said... it won't have anything to do with ASP.Net's cache, following this example in creating your own should be beneficial. Did read my post? – Erik Philips Feb 13 '15 at 8:20
  • Sorry, I overread that part. The problem is, that I already use the Interface and I would like to avoid to rewrite it ;) So, to make it short, the idea is good, but (sadly) not usable in my system :/ – Knerd Feb 13 '15 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.