Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:11:44+00:00 2026-05-29T07:11:44+00:00

I’ve decided to implement a caching facade in one of our applications – the

  • 0

I’ve decided to implement a caching facade in one of our applications – the purpose is to eventually reduce the network overhead and limit the amount of db hits. We are using Castle.Windsor as our IoC Container and we have decided to go with Interceptors to add the caching functionality on top of our services layer using the System.Runtime.Caching namespace.

At this moment I can’t exactly figure out what’s the best approach for constructing the cache key. The goal is to make a distinction between different methods and also include passed argument values – meaning that these two method calls should be cached under two different keys:

IEnumerable<MyObject> GetMyObjectByParam(56); // key1
IEnumerable<MyObject> GetMyObjectByParam(23); // key2

For now I can see two possible implementations:

Option 1:
assembly | class | method return type | method name | argument types | argument hash codes

"MyAssembly.MyClass IEnumerable<MyObject> GetMyObjectByParam(long) { 56 }";

Option 2:
MD5 or SHA-256 computed hash based on the method’s fully-qualified name and passed argument values

string key = new SHA256Managed().ComputeHash(name + args).ToString();

I’m thinking about the first option as the second one requires more processing time – on the other hand the second option enforces exactly the same ‘length’ of all generated keys.

Is it safe to assume that the first option will generate a unique key for methods using complex argument types? Or maybe there is a completely different way of doing this?

Help and opinion will by highly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:11:45+00:00Added an answer on May 29, 2026 at 7:11 am

    Based on some very useful links that I’ve found here and here I’ve decided to implement it more-or-less like this:

    public sealed class CacheKey : IEquatable<CacheKey>
    {
        private readonly Type reflectedType;
        private readonly Type returnType;
        private readonly string name;
        private readonly Type[] parameterTypes;
        private readonly object[] arguments;
    
        public User(Type reflectedType, Type returnType, string name, 
            Type[] parameterTypes, object[] arguments)
        {
            // check for null, incorrect values etc.
    
            this.reflectedType = reflectedType;
            this.returnType = returnType;
            this.name = name;
            this.parameterTypes = parameterTypes;
            this.arguments = arguments;
        }
    
        public override bool Equals(object obj)
        {
            return Equals(obj as CacheKey);
        }
    
        public bool Equals(CacheKey other)
        {
            if (other == null)
            {
                return false;
            }
    
            for (int i = 0; i < parameterTypes.Count; i++)
            {
                if (!parameterTypes[i].Equals(other.parameterTypes[i]))
                {
                    return false;
                }
            }
    
            for (int i = 0; i < arguments.Count; i++)
            {
                if (!arguments[i].Equals(other.arguments[i]))
                {
                    return false;
                }
            }
    
            return reflectedType.Equals(other.reflectedType) &&
               returnType.Equals(other.returnType) &&
               name.Equals(other.name);
        }
    
        private override int GetHashCode()
        {
            unchecked
            {
                int hash = 17;
    
                hash = hash * 31 + reflectedType.GetHashCode();
                hash = hash * 31 + returnType.GetHashCode();
                hash = hash * 31 + name.GetHashCode();
    
                for (int i = 0; i < parameterTypes.Count; i++)
                {
                    hash = hash * 31 + parameterTypes[i].GetHashCode();
                }
    
                for (int i = 0; i < arguments.Count; i++)
                {
                    hash = hash * 31 + arguments[i].GetHashCode();
                }
    
                return hash;
            }
        }
    }
    

    Basically it’s just a general idea – the above code can be easily rewritten to a more generic version with one collection of Fields – the same rules would have to be applied on each element of the collection. I can share the full code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm making a simple page using Google Maps API 3. My first. One marker
I used javascript for loading a picture on my website depending on which small
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.