You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

persistent_cache.cpp 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * \file imperative/src/impl/persistent_cache.cpp
  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. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include <fstream>
  12. #include <string>
  13. #include <vector>
  14. #include "megbrain/imperative/persistent_cache.h"
  15. #include "megbrain/imperative/utils/base64.h"
  16. #include "megbrain/utils/infile_persistent_cache.h"
  17. namespace mgb::imperative::persistent_cache {
  18. class ExtendedInFilePersistentCache final : public ExtendedPersistentCache {
  19. private:
  20. std::string m_path;
  21. std::unique_ptr<mgb::InFilePersistentCache> m_impl;
  22. public:
  23. ExtendedInFilePersistentCache() = default;
  24. bool open(std::string path) {
  25. std::fstream file;
  26. file.open(path, std::ios::in | std::ios::binary);
  27. if (!file.is_open()) {
  28. return false;
  29. }
  30. std::vector<char> bytes = {
  31. std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
  32. if (bytes.size()) {
  33. m_impl = std::make_unique<mgb::InFilePersistentCache>(
  34. (const uint8_t*)bytes.data(), bytes.size());
  35. } else {
  36. m_impl = std::make_unique<mgb::InFilePersistentCache>();
  37. }
  38. m_path = path;
  39. return true;
  40. }
  41. ~ExtendedInFilePersistentCache() {
  42. if (m_impl) {
  43. m_impl->dump_cache(m_path.c_str());
  44. }
  45. }
  46. mgb::Maybe<Blob> get(const std::string& category, const Blob& key) override {
  47. return m_impl->get(category, key);
  48. }
  49. void put(const std::string& category, const Blob& key, const Blob& value) override {
  50. return m_impl->put(category, key, value);
  51. }
  52. std::optional<size_t> clear() override {
  53. m_impl = std::make_unique<mgb::InFilePersistentCache>();
  54. m_impl->dump_cache(m_path.c_str());
  55. return {};
  56. }
  57. bool valid() const override { return m_impl != nullptr; }
  58. };
  59. std::shared_ptr<ExtendedPersistentCache> make_in_file(std::string path) {
  60. auto cache = std::make_shared<ExtendedInFilePersistentCache>();
  61. if (!cache->open(path)) {
  62. return nullptr;
  63. }
  64. return cache;
  65. }
  66. } // namespace mgb::imperative::persistent_cache
  67. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}