LRU.h 3.3 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

G
groot 已提交
18 19 20 21 22 23 24 25 26

#pragma once

#include <unordered_map>
#include <list>
#include <cstddef>
#include <stdexcept>

namespace zilliz {
J
jinhai 已提交
27
namespace milvus {
G
groot 已提交
28 29 30 31 32 33 34 35 36
namespace cache {

template<typename key_t, typename value_t>
class LRU {
public:
    typedef typename std::pair<key_t, value_t> key_value_pair_t;
    typedef typename std::list<key_value_pair_t>::iterator list_iterator_t;
    typedef typename std::list<key_value_pair_t>::reverse_iterator reverse_list_iterator_t;

S
starlord 已提交
37
    LRU(size_t max_size) : max_size_(max_size) {}
G
groot 已提交
38 39

    void put(const key_t& key, const value_t& value) {
S
starlord 已提交
40 41 42 43 44
        auto it = cache_items_map_.find(key);
        cache_items_list_.push_front(key_value_pair_t(key, value));
        if (it != cache_items_map_.end()) {
            cache_items_list_.erase(it->second);
            cache_items_map_.erase(it);
G
groot 已提交
45
        }
S
starlord 已提交
46
        cache_items_map_[key] = cache_items_list_.begin();
G
groot 已提交
47

S
starlord 已提交
48 49
        if (cache_items_map_.size() > max_size_) {
            auto last = cache_items_list_.end();
G
groot 已提交
50
            last--;
S
starlord 已提交
51 52
            cache_items_map_.erase(last->first);
            cache_items_list_.pop_back();
G
groot 已提交
53 54 55 56
        }
    }

    const value_t& get(const key_t& key) {
S
starlord 已提交
57 58
        auto it = cache_items_map_.find(key);
        if (it == cache_items_map_.end()) {
G
groot 已提交
59 60
            throw std::range_error("There is no such key in cache");
        } else {
S
starlord 已提交
61
            cache_items_list_.splice(cache_items_list_.begin(), cache_items_list_, it->second);
G
groot 已提交
62 63 64 65 66
            return it->second->second;
        }
    }

    void erase(const key_t& key) {
S
starlord 已提交
67 68 69 70
        auto it = cache_items_map_.find(key);
        if (it != cache_items_map_.end()) {
            cache_items_list_.erase(it->second);
            cache_items_map_.erase(it);
G
groot 已提交
71 72 73 74
        }
    }

    bool exists(const key_t& key) const {
S
starlord 已提交
75
        return cache_items_map_.find(key) != cache_items_map_.end();
G
groot 已提交
76 77 78
    }

    size_t size() const {
S
starlord 已提交
79
        return cache_items_map_.size();
G
groot 已提交
80 81 82
    }

    list_iterator_t begin() {
S
starlord 已提交
83 84
        iter_ = cache_items_list_.begin();
        return iter_;
G
groot 已提交
85 86 87
    }

    list_iterator_t end() {
S
starlord 已提交
88
        return cache_items_list_.end();
G
groot 已提交
89 90 91
    }

    reverse_list_iterator_t rbegin() {
S
starlord 已提交
92
        return cache_items_list_.rbegin();
G
groot 已提交
93 94 95
    }

    reverse_list_iterator_t rend() {
S
starlord 已提交
96
        return cache_items_list_.rend();
G
groot 已提交
97 98 99
    }

    void clear() {
S
starlord 已提交
100 101
        cache_items_list_.clear();
        cache_items_map_.clear();
G
groot 已提交
102 103 104
    }

private:
S
starlord 已提交
105 106 107 108
    std::list<key_value_pair_t> cache_items_list_;
    std::unordered_map<key_t, list_iterator_t> cache_items_map_;
    size_t max_size_;
    list_iterator_t iter_;
G
groot 已提交
109 110 111
};

}   // cache
J
jinhai 已提交
112
}   // milvus
G
groot 已提交
113 114
}   // zilliz