C++ design: How to cache most recent used

前端 未结 10 1413
走了就别回头了
走了就别回头了 2021-02-09 12:21

We have a C++ application for which we try to improve performance. We identified that data retrieval takes a lot of time, and want to cache data. We can\'t store all data in mem

10条回答
  •  醉酒成梦
    2021-02-09 12:59

    In my approach, it's needed to have a hash-table for lookup stored objects quickly and a linked-list for maintain the sequence of last used.

    When an object are requested. 1) try to find a object from the hash table 2.yes) if found(the value have an pointer of the object in linked-list), move the object in linked-list to the top of the linked-list. 2.no) if not, remove last object from the linked-list and remove the data also from hash-table then put object into hash-table and top of linked-list.

    For example Let's say we have a cache memory only for 3 objects.

    The request sequence is 1 3 2 1 4.

    1) Hash-table : [1] Linked-list : [1]

    2) Hash-table : [1, 3] Linked-list : [3, 1]

    3) Hash-table : [1,2,3] Linked-list : [2,3,1]

    4) Hash-table : [1,2,3] Linked-list : [1,2,3]

    5) Hash-table : [1,2,4] Linked-list : [4,1,2] => 3 out

提交回复
热议问题