Cache.h 2.0 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

#pragma once

S
starlord 已提交
21 22 23
#include "LRU.h"
#include "utils/Log.h"

G
groot 已提交
24 25 26
#include <string>
#include <mutex>
#include <atomic>
S
starlord 已提交
27
#include <set>
G
groot 已提交
28 29

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

S
starlord 已提交
33
template<typename ItemObj>
G
groot 已提交
34
class Cache {
S
starlord 已提交
35
 public:
G
groot 已提交
36
    //mem_capacity, units:GB
G
groot 已提交
37
    Cache(int64_t capacity_gb, uint64_t cache_max_count);
G
groot 已提交
38 39
    ~Cache() = default;

S
starlord 已提交
40 41 42 43 44 45 46
    int64_t usage() const {
        return usage_;
    }

    int64_t capacity() const {
        return capacity_;
    } //unit: BYTE
G
groot 已提交
47
    void set_capacity(int64_t capacity); //unit: BYTE
G
groot 已提交
48

S
starlord 已提交
49 50 51 52 53 54 55
    double freemem_percent() const {
        return freemem_percent_;
    }

    void set_freemem_percent(double percent) {
        freemem_percent_ = percent;
    }
S
starlord 已提交
56

G
groot 已提交
57
    size_t size() const;
S
starlord 已提交
58 59 60 61
    bool exists(const std::string &key);
    ItemObj get(const std::string &key);
    void insert(const std::string &key, const ItemObj &item);
    void erase(const std::string &key);
G
groot 已提交
62 63
    void print();
    void clear();
S
starlord 已提交
64

S
starlord 已提交
65
 private:
G
groot 已提交
66 67
    void free_memory();

S
starlord 已提交
68
 private:
G
groot 已提交
69 70
    int64_t usage_;
    int64_t capacity_;
S
starlord 已提交
71
    double freemem_percent_;
G
groot 已提交
72

S
starlord 已提交
73
    LRU<std::string, ItemObj> lru_;
G
groot 已提交
74 75 76
    mutable std::mutex mutex_;
};

S
starlord 已提交
77 78 79
} // namespace cache
} // namespace milvus
} // namespace zilliz
G
groot 已提交
80

S
starlord 已提交
81
#include "cache/Cache.inl"