缓存功能线程安全/死锁安全的高流量站点?

时间:2022-12-17 11:48:11

Are the functions below a good practice for high traffice sites hitting these? Is there any type of downfall with deadlocks or thread safe issues?

以下的功能是否适合繁忙的交通枢纽?是否存在死锁或线程安全问题的任何类型的故障?

public static T GetInitializedTempCache<T>(string key, Func<T> getData, int minutes, bool skip = false)
{
    if (!skip)
    {
        var value = HttpRuntime.Cache[key];
        if (value == null || value.GetType() != typeof(T))
        {
            T data = getData();
            if (data != null && Config.Debugging.EnableCaching)
            {
                HttpRuntime.Cache.Add(key, data, null, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
            }
            return data;
        }
        return (T)value;
    }
    else
    {
        return getData();
    }
}

public static void RemoveTempCacheContains(string key)
{
    var keys = new List<string>();

    var enumerator = HttpRuntime.Cache.GetEnumerator();

    // copy all keys that currently exist in Cache
    while (enumerator.MoveNext())
    {
        var cacheKey = (string)enumerator.Key;

        if (cacheKey.Contains(key))
        {
            keys.Add(cacheKey);
        }
    }

    for (int i = 0; i < keys.Count; i++)
    {
        HttpRuntime.Cache.Remove(keys[i]);
    }
}

1 个解决方案

#1


3  

To answer your question for good practices for high traffic sites. Yes, caching long running IO request is a good practice, and making the caching mechanism generic is also a good practice. Two suggestion though, change add to insert since you most definitely will run into a race condition. insert will always insert the latest whereas add would always add the first. Second, since you mentioned high traffic, consider an async func alternative. Async, would allow the long running IO functions to not block the request threads.

回答你的问题,为高流量网站的良好实践。是的,缓存长时间运行的IO请求是一个很好的实践,使缓存机制通用也是一个很好的实践。有两个建议,更改add to insert,因为您肯定会遇到竞态条件。插入总是插入最新的,而添加总是添加第一个。其次,既然您提到了高通信量,请考虑使用异步方式。异步,允许长时间运行的IO函数不阻塞请求线程。

To answer your question for downfalls. You aren't using any locks in this code, so you shouldn't run into any deadlocks. getData() I imagine doesn't have any locks either and is just doing some long running IO. HttpRuntime.Cache is also thread safe. So unless getData() is not thread safe or has locks, this should be fine.

回答你的下降问题。在此代码中不使用任何锁,因此不应该遇到任何死锁。我想getData()也没有任何锁,只是做一些长时间运行的IO。HttpRuntime。缓存也是线程安全的。因此,除非getData()不是线程安全的,或者有锁,这应该没问题。

#1


3  

To answer your question for good practices for high traffic sites. Yes, caching long running IO request is a good practice, and making the caching mechanism generic is also a good practice. Two suggestion though, change add to insert since you most definitely will run into a race condition. insert will always insert the latest whereas add would always add the first. Second, since you mentioned high traffic, consider an async func alternative. Async, would allow the long running IO functions to not block the request threads.

回答你的问题,为高流量网站的良好实践。是的,缓存长时间运行的IO请求是一个很好的实践,使缓存机制通用也是一个很好的实践。有两个建议,更改add to insert,因为您肯定会遇到竞态条件。插入总是插入最新的,而添加总是添加第一个。其次,既然您提到了高通信量,请考虑使用异步方式。异步,允许长时间运行的IO函数不阻塞请求线程。

To answer your question for downfalls. You aren't using any locks in this code, so you shouldn't run into any deadlocks. getData() I imagine doesn't have any locks either and is just doing some long running IO. HttpRuntime.Cache is also thread safe. So unless getData() is not thread safe or has locks, this should be fine.

回答你的下降问题。在此代码中不使用任何锁,因此不应该遇到任何死锁。我想getData()也没有任何锁,只是做一些长时间运行的IO。HttpRuntime。缓存也是线程安全的。因此,除非getData()不是线程安全的,或者有锁,这应该没问题。