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

#pragma once

#include <unordered_map>
#include <list>
#include <cstddef>
#include <stdexcept>
S
starlord 已提交
25
#include <utility>
G
groot 已提交
26 27

namespace zilliz {
J
jinhai 已提交
28
namespace milvus {
G
groot 已提交
29 30 31 32
namespace cache {

template<typename key_t, typename value_t>
class LRU {
S
starlord 已提交
33
 public:
G
groot 已提交
34 35 36 37
    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 已提交
38 39
    explicit LRU(size_t max_size) : max_size_(max_size) {
    }
G
groot 已提交
40

S
starlord 已提交
41
    void put(const key_t &key, const value_t &value) {
S
starlord 已提交
42 43 44 45 46
        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 已提交
47
        }
S
starlord 已提交
48
        cache_items_map_[key] = cache_items_list_.begin();
G
groot 已提交
49

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

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

S
starlord 已提交
68
    void erase(const key_t &key) {
S
starlord 已提交
69 70 71 72
        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 已提交
73 74 75
        }
    }

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

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

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

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

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

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

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

S
starlord 已提交
106
 private:
S
starlord 已提交
107 108 109 110
    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 已提交
111 112
};

S
starlord 已提交
113 114 115
} // namespace cache
} // namespace milvus
} // namespace zilliz
G
groot 已提交
116