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

  • Home
  • SEARCH
  • 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 8081745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:50:05+00:00 2026-06-05T16:50:05+00:00

Is there any generic alternative / implementation for MemoryCache? I know that a MemoryCache

  • 0

Is there any generic alternative / implementation for MemoryCache?

I know that a MemoryCache uses a Hashtable under the hood, so all it would take is to transition into using a Dictionary<,>, which is the generic version of a Hashtable.

This would provide type safety and provide performance benefits as no boxing/unboxing.

EDIT: Another thing I’m interested in is having a different key type. The default is a System.String.

  • 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-06-05T16:50:07+00:00Added an answer on June 5, 2026 at 4:50 pm

    Is there any generic alternative / implementation for MemoryCache?

    Not in the base class library. You’d have to roll your own, though I, personally, would just make a wrapper around MemoryCache that provides the API you wish.

    This would provide type safety and provide performance benefits as no boxing/unboxing

    The type safety can be handled fairly easily in a wrapper class. The boxing/unboxing would only be an issue if you were storing value types (not classes), and even then, would likely be minimal, as it’s unlikely that you’re pushing and pulling from cache often enough to have this be a true performance issue.

    As for type safety and usability, I’ve actually written my own methods to wrap the MemoryCache item’s calls in a generic method, which allows a bit nicer usage from an API standpoint. This is very easy – typically just requires a method like:

    public T GetItem<T>(string key) where T : class
    {
        return memoryCache[key] as T;
    }
    

    Similarly, you can make a method to set values the same way.

    EDIT: Another thing I’m interested in is having a different key type. The default is a System.String.

    This is not supported directly with MemoryCache, so it would require a fair bit of work to make your own key generation. One option would be to make a type safe wrapper which also provided a Func<T, string> to generate a string key based off your value – which would allow you to generate a cache entry for any type T. You’d have to be careful, of course, to include all data in the string that you wanted as part of your comparison, however.

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

Sidebar

Related Questions

Is there any possible way that a generic type can be used to contain
Is there any way to store the generic parameter type passed in at construction
Is there any name for the following DB table design: Basically we have generic
Let's imagine I have instantiated generic type by Activator.CreateInstance. Is there any way to
I've seen GDIFF(Generic Diff Format) in wikipedia , and I wander is there any
have you any alternative to these code, i want a more generic code I
is there any generic way to get the role which is required for some
Are there any alternative to the URLDownloadToFile function? I cannot download anything from an
Is there any way to get a List<string> which contains all 'usings' within a
Is there any way to change the BackColor of the border of a panel

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.