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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:27:54+00:00 2026-05-23T13:27:54+00:00

I have a bank/collection which caches instances of objects in memory so that each

  • 0

I have a bank/collection which caches instances of objects in memory so that each request doesn’t need to go back to the datastore. I’d like Autofac to provide an instance of this bank, but then expire it after x seconds, so that a new instance is created on the next request. I’m having trouble getting my head around setting up a LifetimeScope to achieve this. I’ve read through this a couple of times. The Bank object is not really subject to a unit of work. It will ideally reside ‘above’ all units of work, caching objects within and across them.

I’m currently using the approach below, however it isn’t working as I’d hoped.

Can someone please point me in the right direction?

....
builder.Register(c =>
            {
                return new ORMapBank(c.Resolve<IORMapRoot>());
            }).InstancePerMatchingLifetimeScope(ExpireTimeTag.Tag());


        IContainer container = builder.Build();
        var TimedCache= RootScope.BeginLifetimeScope(ExpireTimeTag.Tag());
        DependencyResolver.SetResolver(new AutofacDependencyResolver(TimedCache));

….

public static class ExpireTimeTag
{
    static DateTime d = DateTime.Now;
    static Object tag = new Object();

    public static object Tag()
    {
        if (d.AddSeconds(10) < DateTime.Now)
        {
            CreateTag();
        return tag;
    }

    private static void CreateTag()
    {
        tag = new Object();
    }
}

Thanks very much in advance.

  • 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-23T13:27:55+00:00Added an answer on May 23, 2026 at 1:27 pm

    It is common to use a caching decorator to achieve this kind of behaviour. Assuming your IORMapRoot is responsible for getting the data in question (but it would work the same if ORMapBank) you do the following:

    • Create a new type, CachingORMapRoot that implements IORMapRoot
    • Add a constructor that takes the expiry TimeSpan and an instance of the original IORMapRoot implementation.
    • Implement the members to call the underlying instance and then cache the results accordingly for subsequent calls (implementation will vary on your cache technology).
    • Register this type in the container as IORMapRoot

    This is a very clean way to implement such caching. It also makes it easy to switch between cached and non-cached implementations.

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

Sidebar

Related Questions

I have a Bank Object that has several nested objects/properties/methods as well as wrapping
I have a bank of pdfs on my server which when downloaded need text
I have a file from bank that is structure in very special way. Where
I have many text files containing bank transactions in the middle of them. Each
I have a function that shrinks the size of a Food bank (represented by
I have a class called WorkingDays, this class requires a collection of dates that
i have 3 tables each as follow cash_credit Bank_Name-------in_date-------Com_Id---Amount America Bank 15/05/2010 1 200
I have a database of academic references which are part of a common bank
I have a bank account number that is variable length. 1234567890123 0987654321 I want
I have the AJAX-code, which calls to controller of CodeIgniter's back-end: <script> $(document).ready(function(){ $(#select_bank).change(function(){

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.