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 9273699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:10:32+00:00 2026-06-18T16:10:32+00:00

I’m writing a fluent registration for a cache provider, but for some reason my

  • 0

I’m writing a fluent registration for a cache provider, but for some reason my generics are not happy. I’m getting an error on this bit: value = _loadFunction();

Cannot implicitly convert type 'T' to 'T [HttpRuntimeCache.cs(10)]

Code Below:

using IDM.CMS3.Service.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace IDM.CMS3.Web.Public.CacheProviders
{
    public class HttpRuntimeCache<T> : IFluentCacheProvider<T>
    {
        string _key;
        Func<T> _loadFunction;
        DateTime? _absoluteExpiry;
        TimeSpan? _relativeExpiry;

        public HttpRuntimeCache()
        {

        }

        public IFluentCacheProvider<T> Key(string key)
        {
            _key = key;
            return this;
        }

        public IFluentCacheProvider<T> Load(Func<T> loadFunction)
        {
            _loadFunction = loadFunction;
            return this;
        }

        public IFluentCacheProvider<T> AbsoluteExpiry(DateTime absoluteExpiry)
        {
            _absoluteExpiry = absoluteExpiry;
            return this;
        }

        public IFluentCacheProvider<T> RelativeExpiry(TimeSpan relativeExpiry)
        {
            _relativeExpiry = relativeExpiry;
            return this;
        }

        public T Value()
        {
            return FetchAndCache<T>();
        }

        public void InvalidateCacheItem(string cacheKey)
        {
            throw new NotImplementedException();
        }

        T FetchAndCache<T>()
        {
            T value;
            if (!TryGetValue<T>(_key, out value))
            {
                value = _loadFunction();
                if (!_absoluteExpiry.HasValue)
                    _absoluteExpiry = Cache.NoAbsoluteExpiration;

                if (!_relativeExpiry.HasValue)
                    _relativeExpiry = Cache.NoSlidingExpiration;

                HttpContext.Current.Cache.Insert(_key, value, null, _absoluteExpiry.Value, _relativeExpiry.Value);

            }
            return value;
        }

        bool TryGetValue<T>(string key, out T value)
        {
            object cachedValue = HttpContext.Current.Cache.Get(key);
            if (cachedValue == null)
            {
                value = default(T);
                return false;
            }
            else
            {
                try
                {
                    value = (T)cachedValue;
                    return true;
                }
                catch
                {
                    value = default(T);
                    return false;
                }
            }
        }
    }
}
  • 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-18T16:10:34+00:00Added an answer on June 18, 2026 at 4:10 pm

    The T FetchAndCache<T> and bool TryGetValue<T> are redefining a new T type separate from the one declared on the class level. I think once you remove the extra generic declaration it should work fine. That is, rewrite them to be:

    T FetchAndCache()
    {
         ...
    }
    
    bool TryGetValue(string key, out T value)
    {
         ...
    }
    

    Once you do that, the compiler will recognize the T here as the one declared on the class.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.