开发者

ASP.NET Caching, what would be a best practice

开发者 https://www.devze.com 2023-01-11 17:31 出处:网络
I have a table that stores songs and plays counter. Here\'s the code: //Increse plays counter string ip = Request.UserHostAddress;

I have a table that stores songs and plays counter.

Here's the code:

//Increse plays counter
string ip = Request.UserHostAddress;
if (!string.IsNullOrEmpty(ip))
{
    if (Cache["plays:" + trackID + ":" + ip] == null)
    {
        tracks.IncreasePlaysCounter(trackID);
        Cache["plays:" + trackID + ":" + ip开发者_开发百科] = true;
    }
}

I wonder what would be a better practice, store many cache items(like this) or store one item like a ArrayList that would contain the users who already heard that song. Is there any difference?


Neither. If your system is in any way successful it will have far too many individual entries being written for this to be able to scale to cope, especially when you add the locking code to deal with the number of simultaneous writes and reads this will have to cope with.

Store this information in a database. Or don't store it at all, but obtain it by parsing the webserver logs (if the trackID is a query parameter, this information will already be in those logs without any work from you).


I'm going to approach this question from the angle purely of caching rather than the context of plays and tracks.

When deciding to cache data, it's important to consider what the dependencies are and how the cache expires.

If the cache of each user can be expired individually than how you have it is fine. If it turns out that one record becoming expired should invalidate the entire set of records, then put it into an ArrayList and dump the whole object.

0

精彩评论

暂无评论...
验证码 换一张
取 消