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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:26:28+00:00 2026-05-27T07:26:28+00:00

I have such a class: public static class CacheManager { static object lockObject =

  • 0

I have such a class:

public static class CacheManager
{
    static object lockObject = new object();

    static MemcachedClient CacheObject
    {
        get
        {
            if (!MemcachedClient.Exists(Settings.Default
                .CacheInstanceName))
            {
                MemcachedClient.Setup(Settings.Default
                    .CacheInstanceName,
                        new string[] {Settings.Default
                            .CacheHostAddress});
            }
            //
            //
            return MemcachedClient.GetInstance(Settings
                .Default.CacheInstanceName);
        }
    }

    public static List<TData> Get<TData>(string key, Func<List<int>> getListCallback,
        Func<int, TData> getItemCallback) where TData : class
    {
        var result = new List<TData>();
        //
        //
        var list = CacheObject.Get(key);
        if (list == null)
        {
            lock (lockObject)
            {
                list = CacheObject.Get(key);
                if (list == null)
                {
                    list = getListCallback();
                    CacheObject.Set(key, list);
                    //
                    //
                    foreach (var id in (List<int>)list)
                    {
                        var item = getItemCallback(id);
                        result.Add(item);
                        CacheObject.Set(string.Concat(key, id), item);
                    }
                }
            }
        }
        else
        {
            foreach (var id in (List<int>)list)
            {
                var itemKey = string.Concat(key, id);
                //
                //
                var item = CacheObject.Get(itemKey);
                if (item == null)
                {
                    lock (lockObject)
                    {
                        item = CacheObject.Get(itemKey);
                        if (item == null)
                        {
                            item = getItemCallback(id);
                            CacheObject.Set(itemKey, item);
                        }
                    }
                }
                //
                //
                result.Add((TData)item);
            }
        }
        //
        //
        return (List<TData>)result;
    }

    public static void Remove(string key)
    {
        CacheObject.Delete(key);
    }
}

it is used in classes-repositories:

public class NewsRepository : BaseRepository, IRepository
{

    public List<News> FindAll()
    {
        return CacheManager.Get<News>(key,
            () => clientEntities.News.OrderByDescending(n => n.DateCreated).Select(n => n.NewsId).ToList(),
            (id) => clientEntities.News.Single(n => n.NewsId == id));
    }
}
public class PagesRepository : BaseRepository
{
    public List<Page> FindAll()
    {

        return CacheManager.Get<Page>(key,
            () => clientEntities.Pages.OrderBy(p => p.PageId).Select(p => p.PageId).ToList(),
            (id) => clientEntities.Pages.Single(p => p.PageId == id));
    }
}

my question is: for example NewsRepository didn’t find news in cache and got the lock and began to load data but at this moment PagesRepository didn’t find pages in cache. will PagesRepository’s CacheManager be locked by NewsRepository or (I think so) NewsRepository’s CacheManager is another static class and its internal locks do not touch PagesRepository’s CacheManager?

  • 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-27T07:26:29+00:00Added an answer on May 27, 2026 at 7:26 am

    A static field of a non-generic type (that is itself not nested in a generic type etc) exists only once, so all the locks will conflict.

    If (comments) your aim is to make the lock per-type (from the generic method), then perhaps the best way to do that is:

    public static class CacheManager {
        static class TypeLock<T> {
            public static readonly object SyncLock = new object();
        }
        ...
        void SomeGenericMethod<TData>(args) {
           ...
           lock(TypeLock<TData>.SyncLock) {
              ...
           }
           ...
        }
    }
    

    Here, SyncLock exists once (and only once) per T, so per TData. This allows you to keep your existing API (where CacheManager is non-generic).

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

Sidebar

Related Questions

I have such class public unsafe class EigenSolver { public double* aPtr {get; private
I have such a class: public class Cycle { public List<int> Edges { get;
In AS3, if I have a class such: public class dude { //default value
Say I have a simple object such as class Something { public int SomeInt
So let's say we have a domain object such as the following public class
If I have a simple class such as:- @XmlRootElement public class MyClass { @XmlAttribute(required=true)
I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
If I have a base class such that public abstract class XMLSubscription <T extends
Let's say I have a regular simple Java class, such as: public class Foo
If I have a PHP class such as this one: class A { public

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.