redis_cache.h 1.5 KB
Newer Older
1 2
/**
 * \file lite/src/mge/algo_cache/redis_cache.h
3
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
4
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6
 *
7 8 9
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 */

#pragma once

#include "lite_build_config.h"

#if !defined(WIN32) && LITE_BUILD_WITH_MGE && LITE_WITH_CUDA
#include <cpp_redis/cpp_redis>
#include <string>
#include <vector>
#include "megbrain/utils/persistent_cache.h"

namespace lite {

//! TODO: fix one thread set cache when other threads is using old cache
class RedisCache final : public mgb::PersistentCache {
public:
    RedisCache(std::string redis_ip, size_t port, std::string password);

    bool is_valid() { return m_client.is_connected(); }
    ~RedisCache() {}
    void init(std::shared_ptr<mgb::PersistentCache> old) { m_old = old; }

    mgb::Maybe<Blob> get(const std::string& category, const Blob& key) override;

    void put(const std::string& category, const Blob& key,
             const Blob& value) override;

private:
    std::shared_ptr<mgb::PersistentCache> m_old;
    LITE_MUTEX m_mtx;
    cpp_redis::client m_client;
    const std::string m_ip;
    const size_t m_port;
    const std::string m_password;
};

}  // namespace lite
#endif
// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}