CacheMgr.inl 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

S
starlord 已提交
19

J
jinhai 已提交
20
namespace milvus {
G
groot 已提交
21 22
namespace cache {

S
starlord 已提交
23 24
template<typename ItemObj>
CacheMgr<ItemObj>::CacheMgr() {
G
groot 已提交
25 26
}

S
starlord 已提交
27 28
template<typename ItemObj>
CacheMgr<ItemObj>::~CacheMgr() {
G
groot 已提交
29

G
groot 已提交
30 31
}

S
starlord 已提交
32
template<typename ItemObj>
S
starlord 已提交
33 34 35
uint64_t
CacheMgr<ItemObj>::ItemCount() const {
    if (cache_ == nullptr) {
J
jinhai 已提交
36
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
37 38 39
        return 0;
    }

S
starlord 已提交
40
    return (uint64_t) (cache_->size());
G
groot 已提交
41 42
}

S
starlord 已提交
43
template<typename ItemObj>
S
starlord 已提交
44 45 46
bool
CacheMgr<ItemObj>::ItemExists(const std::string &key) {
    if (cache_ == nullptr) {
J
jinhai 已提交
47
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
48 49 50 51 52 53
        return false;
    }

    return cache_->exists(key);
}

S
starlord 已提交
54
template<typename ItemObj>
S
starlord 已提交
55 56 57
ItemObj
CacheMgr<ItemObj>::GetItem(const std::string &key) {
    if (cache_ == nullptr) {
J
jinhai 已提交
58
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
59 60
        return nullptr;
    }
Y
yu yunfeng 已提交
61
    server::Metrics::GetInstance().CacheAccessTotalIncrement();
G
groot 已提交
62 63 64
    return cache_->get(key);
}

S
starlord 已提交
65
template<typename ItemObj>
S
starlord 已提交
66 67 68
void
CacheMgr<ItemObj>::InsertItem(const std::string &key, const ItemObj &data) {
    if (cache_ == nullptr) {
J
jinhai 已提交
69
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
70 71 72 73
        return;
    }

    cache_->insert(key, data);
Y
yu yunfeng 已提交
74
    server::Metrics::GetInstance().CacheAccessTotalIncrement();
G
groot 已提交
75 76
}

S
starlord 已提交
77
template<typename ItemObj>
S
starlord 已提交
78 79 80
void
CacheMgr<ItemObj>::EraseItem(const std::string &key) {
    if (cache_ == nullptr) {
J
jinhai 已提交
81
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
82 83 84 85
        return;
    }

    cache_->erase(key);
Y
yu yunfeng 已提交
86
    server::Metrics::GetInstance().CacheAccessTotalIncrement();
G
groot 已提交
87 88
}

S
starlord 已提交
89
template<typename ItemObj>
S
starlord 已提交
90 91 92
void
CacheMgr<ItemObj>::PrintInfo() {
    if (cache_ == nullptr) {
J
jinhai 已提交
93
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
94 95 96 97 98 99
        return;
    }

    cache_->print();
}

S
starlord 已提交
100
template<typename ItemObj>
S
starlord 已提交
101 102 103
void
CacheMgr<ItemObj>::ClearCache() {
    if (cache_ == nullptr) {
J
jinhai 已提交
104
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
105 106 107 108 109 110
        return;
    }

    cache_->clear();
}

S
starlord 已提交
111
template<typename ItemObj>
S
starlord 已提交
112 113 114
int64_t
CacheMgr<ItemObj>::CacheUsage() const {
    if (cache_ == nullptr) {
J
jinhai 已提交
115
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
116 117 118 119 120 121
        return 0;
    }

    return cache_->usage();
}

S
starlord 已提交
122
template<typename ItemObj>
S
starlord 已提交
123 124 125
int64_t
CacheMgr<ItemObj>::CacheCapacity() const {
    if (cache_ == nullptr) {
J
jinhai 已提交
126
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
127 128 129 130 131 132
        return 0;
    }

    return cache_->capacity();
}

S
starlord 已提交
133
template<typename ItemObj>
S
starlord 已提交
134 135 136
void
CacheMgr<ItemObj>::SetCapacity(int64_t capacity) {
    if (cache_ == nullptr) {
J
jinhai 已提交
137
        SERVER_LOG_ERROR << "Cache doesn't exist";
G
groot 已提交
138 139 140 141 142
        return;
    }
    cache_->set_capacity(capacity);
}

S
starlord 已提交
143 144
} // namespace cache
} // namespace milvus
S
starlord 已提交
145