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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:26:53+00:00 2026-05-22T11:26:53+00:00

How can I implement a CacheAttribute so I can have the same logic for

  • 0

How can I implement a CacheAttribute so I can have the same logic for all properties in one place?

I am doing this to cache

[TestClass]
public class All
{
    public string N
    {
        get
        {
            var value =
                MemoryCache.Default.Get("Website.Tests.All.N") as string;

            if (value != null)
            {
                return value;
            }

            value = DateTime.Now.ToString("HH:mm:ss.ffff");

            MemoryCache.Default.Add("Website.Tests.All.N", value,
                new CacheItemPolicy
                {
                    AbsoluteExpiration =
                        new DateTimeOffset(DateTime.Now.AddSeconds(5))
                });

            return value;
        }
    }

    [TestMethod]
    public void Extension()
    {
        var start = N;

        Thread.Sleep(1000);

        var end = N;

        Assert.AreEqual(start, end);
    }
}

And I want to use this instead

[TestClass]
public class All
{
    [Cache(Duration=5000)]
    public string N
    {
        get
        {
            return DateTime.Now.ToString("HH:mm:ss.ffff");
        }
    }

    [TestMethod]
    public void Extension()
    {
        var start = N;

        Thread.Sleep(1000);

        var end = N;

        Assert.AreEqual(start, end);
    }
}

Is there a way to make this syntax sugar?

  • 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-22T11:26:54+00:00Added an answer on May 22, 2026 at 11:26 am

    It seems you want to “take over” the get method for the property so as to inspect a cache first before returning the “actual” value from your property. Since there is no way to perform property interception on an ad-hoc basis, you’ll have to plan ahead for this facility.

    One approach would be to use interfaces and write your classes to the interface. For example,

    public interface IAll 
    {
        string N { get; set; }
    }
    

    Now you can use proxies to create a wrapper around the original instance of All that also implements this interface. Furthermore, since you’re fully in charge of the property now, you can check the cache whenever the getter is called. Since you’ll have the PropertyInfo/MethodInfo at your disposal, you should have no trouble generating a unique key per property.

    So, whenever you would instantiate an instance of All, you also instantiate this proxy, passing it the instance of All. All subsequent usages of that instance of All should instead be passed the proxy. Like any class factory implementation, this requires you to forgo use of the new operator.

    Alternatively, you can use virtual methods instead of interfaces.

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

Sidebar

Related Questions

This is how I understand I can implement the singleton pattern in C#: public
i can implement the QSB on my app using onSearchRequested() method.i have 4 column
How we can implement Two recaptcha user control on the same page. Problem :
official android dev website says that:「You can implement your tab content in one of
In C#, I can implement a generic interface twice on one class, using two
As noticed in this question: Randomize a List<T> you can implement a shuffle method
UITapGestureRecognizer can implement with array of image? can it work same as single image?
How can implement this getter and setter? will it work with the methods instead
I've been trying to determine whether I can implement AJAX & SSL, so this
I want to know how I can implement membership provider class to have ability

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.