一个缓存系统的算法?

lanying 2004-08-11 11:09:01
现在需要实现一个缓存系统,针对某个特定存储过程
如果2次执行存储过程的参数相同,则从缓存取,否则执行,并更新缓存

参数有多个,每个参数固定数目的值

现在的问题是,如何设计算法使得检索缓存有较高的效率?
...全文
214 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ThinkOut 2004-10-16
  • 打赏
  • 举报
回复
帮你UP下!
101monster 2004-10-08
  • 打赏
  • 举报
回复
呵呵,UP!
短歌如风 2004-10-08
  • 打赏
  • 举报
回复
你可以用一个类把参数封装起来,作为一个类型。
struct my_param_t
{
int first;
double second;
std::string third;
};
typedef simple_buffer<my_param_t, std::string, std::string (*)(my_param_t const &)> my_buffer_t;
如果无法为它设计一个优秀的散列函数,那就用std::map,只要为它定义一个表达顺序关系的operator < 就可以了:
inline bool operator < (my_param_t const & compared, my_param_t const & compare_to)
{
return (compared.first < compare_to.first) ||
((compared.first == comapre_to.first) && (compared.second < compare_to.second)) ||
((compared.first == compare_to.first) && (compared.second == compare_to.second) && (compared.third < compare_to.third));
}
lanying 2004-09-22
  • 打赏
  • 举报
回复
up
lanying 2004-09-14
  • 打赏
  • 举报
回复
谢谢楼上,但是我的这个存储过程参数比较多,而且参数之间有一定的关系
所以我不知道怎么设计哈希表,希望楼上能给予指导
lanying 2004-09-02
  • 打赏
  • 举报
回复
谢谢楼上!
短歌如风 2004-09-02
  • 打赏
  • 举报
回复
当 然 这 种 从 缓 存 中 删 除 过 期 内 容 时 的 算 法 比 较 简 单 , 不 过 一 般 情 况 下 是 可 以 用 的 , 优 点 是 代 码 简 单 。 如 果 不 满 意 , 还 可 以 设 计 复 杂 一 些 , 关 键 是 新 加 入 的 内 容 和 刚 访 问 的 内 容 应 该 插 入 到 线 性 表 的 哪 个 位 置 。
短歌如风 2004-09-02
  • 打赏
  • 举报
回复
我大致写了一个简单的实现,楼主可以参考一下:
#include <iostream>
#include <list>
#include <map>
#include <utility>

template<typename param_type, typename result_type, typename processor_type>
class simple_buffer
{
public:
typedef simple_buffer<param_type, result_type, processor_type> this_type;
typedef std::pair<param_type, result_type> list_item_type;
typedef std::list<list_item_type> list_type;
typedef std::map<param_type, typename list_type::iterator> map_type;
private:
const processor_type processor;
list_type list;
map_type map;
const unsigned int max_len;
public:
simple_buffer(unsigned int the_max_len, const processor_type& the_processor)
:max_len(the_max_len),
processor(the_processor)
{
}
result_type process(const param_type& param)
{
map_type::iterator it = map.find(param);
list_type::iterator in_list;
if (it != map.end())
{
in_list = it->second;
list.push_front(*in_list);
list.erase(in_list);
in_list = list.begin();
it->second = in_list;
}
else
{
list.push_front(list_item_type(param, processor(param)));
in_list = list.begin();
map.insert(map_type::value_type(param, in_list));
if (list.size() > max_len)
{
list_type::iterator to_be_erase = list.end();
--to_be_erase;
map.erase(to_be_erase->first);
list.pop_back();
}
}
return in_list->second;
}
result_type operator()(const param_type& param){return process(param);}
};

int test(int a)
{
std::cout << "test is called with parameter of " << a << std::endl;
return a * 10;
}
int main()
{
simple_buffer<int, int, int (*)(int)> buffer(2, test);
std::cout << "\tbuffer(1)=" << buffer(1) << std::endl;
std::cout << "\tbuffer(1)=" << buffer(1) << std::endl;
std::cout << "\tbuffer(2)=" << buffer(2) << std::endl;
std::cout << "\tbuffer(1)=" << buffer(1) << std::endl;
std::cout << "\tbuffer(2)=" << buffer(2) << std::endl;
std::cout << "\tbuffer(1)=" << buffer(1) << std::endl;
std::cout << "\tbuffer(2)=" << buffer(2) << std::endl;
std::cout << "\tbuffer(1)=" << buffer(1) << std::endl;
std::cout << "\tbuffer(3)=" << buffer(3) << std::endl;
std::cout << "\tbuffer(2)=" << buffer(2) << std::endl;
std::cout << "\tbuffer(3)=" << buffer(3) << std::endl;
std::cout << "\tbuffer(2)=" << buffer(2) << std::endl;
std::cout << "\tbuffer(1)=" << buffer(1) << std::endl;
}
短歌如风 2004-09-02
  • 打赏
  • 举报
回复
应该用一个链表存储所有<参数,结果>,用一个<参数,链表节点指针>的字典来作为索引。
当一次调用在字典中找到时,把对应的链表节点移到表首(之所以用链表就是为了这步操作,顺序表不适于这种操作),然后取出结果并返回。
当一次调用在字典中没有找到时:
调用存储过程;
把结果放到链表首;
把<参数,链表新节点指针>加入到字典
如果链表长度超过限制,则从字典中删除链表尾相对应的项,并删除链表尾。
字典的实现可以用hash(必须用桶散列,因为要删除),也可以用BST。

如果是在C++中实现,可以直接使用std::list<std::pair<param_type, result_type> >和std::map<param_type, std::list<std::pair<param_type, result_type> >::iterator>.
lanying 2004-09-01
  • 打赏
  • 举报
回复
这个版人气太差了
zzwu 2004-08-26
  • 打赏
  • 举报
回复
帮你up一下.
lanying 2004-08-25
  • 打赏
  • 举报
回复
up

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧