缓存策略 [英] Caching strategy

查看:72
本文介绍了缓存策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从启动时加载的服务器中获取了大量静态数据.我将其加载到foo对象的哈希表中.用户有一个下拉菜单,他们可以在其中选择这些对象之一.默认情况下,有50,000个对象占用大量内存.我正在尝试减少内存

i have a large list of static data from a server that i load on startup. I load this into a hashtable of foo objects. the user has a dropdown where they can choose one of these object. by default there are 50,000 objects which take up a lot of memory. i am trying to reduce memory

在监视使用情况之后,事实证明大多数人只使用其中的1,000个.我想拥有它,以便gui只加载那1000个,如果他们需要选择那1000个之外的那一个,那么他们可以返回服务器或磁盘.

after monitoring usage, it turns out that most people only use about 1,000 of them. i want to have it so the gui only loads up those 1000 and if they need to select one that is outside that 1000 then they can go back to the server or to disk.

什么是最好的方法?

推荐答案

我假设您正在其他地方过滤此列表...否则,这就是一个列表.在访问数据库之前,访问新的,精简的静态缓存的最简单方法是通过一个方法传递所有请求:

I'm assuming you're filtering this list elsewhere...otherwise that's quite a list. The easiest way to hit your new, leaner static cache before hitting the DB will be to pass all requests through a single method:

public yourClass GetDesiredObject(string lkupValue)
{
  if (yourCachedHashtable.ContainsKey(lkupValue))
  {
     return yourCachedHashtable[lkupValue]
  }
  else
  {
     //Hit the db to retrieve the object values.
     yourClass obj = yourDatabaseCode.GetNewObject(lkupValue);
     //Add to the cache if desired.
     yourCachedHashtable.Add(lkupValue, obj);
     return obj;
  }
}

这篇关于缓存策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆