1
#pragma once
2

            
3
#include "source/common/protobuf/utility.h"
4
#include "source/extensions/filters/http/cache_v2/http_cache.h"
5

            
6
#include "absl/base/thread_annotations.h"
7
#include "absl/container/flat_hash_map.h"
8
#include "absl/synchronization/mutex.h"
9

            
10
namespace Envoy {
11
namespace Extensions {
12
namespace HttpFilters {
13
namespace CacheV2 {
14

            
15
// Example cache backend that never evicts. Not suitable for production use.
16
class SimpleHttpCache : public HttpCache {
17
public:
18
  class Entry {
19
  public:
20
    Entry(Http::ResponseHeaderMapPtr response_headers, ResponseMetadata metadata)
21
101
        : response_headers_(std::move(response_headers)), metadata_(std::move(metadata)) {}
22
    Buffer::InstancePtr body(AdjustedByteRange range) const;
23
    void appendBody(Buffer::InstancePtr buf);
24
    uint64_t bodySize() const;
25
    Http::ResponseHeaderMapPtr copyHeaders() const;
26
    Http::ResponseTrailerMapPtr copyTrailers() const;
27
    ResponseMetadata metadata() const;
28
    void updateHeadersAndMetadata(Http::ResponseHeaderMapPtr response_headers,
29
                                  ResponseMetadata metadata);
30
    void setTrailers(Http::ResponseTrailerMapPtr trailers);
31
    void setEndStreamAfterBody();
32

            
33
  private:
34
    mutable absl::Mutex mu_;
35
    // Body can be being written to while being read from, so mutex guarded.
36
    std::string body_ ABSL_GUARDED_BY(mu_);
37
    Http::ResponseHeaderMapPtr response_headers_ ABSL_GUARDED_BY(mu_);
38
    ResponseMetadata metadata_ ABSL_GUARDED_BY(mu_);
39
    bool end_stream_after_body_{false};
40
    Http::ResponseTrailerMapPtr trailers_;
41
  };
42

            
43
  // HttpCache
44
  CacheInfo cacheInfo() const override;
45
  void lookup(LookupRequest&& request, LookupCallback&& callback) override;
46
  void evict(Event::Dispatcher& dispatcher, const Key& key) override;
47
  // Touch is to influence expiry, this implementation has no expiry.
48
212
  void touch(const Key&, SystemTime) override {}
49
  void updateHeaders(Event::Dispatcher& dispatcher, const Key& key,
50
                     const Http::ResponseHeaderMap& updated_headers,
51
                     const ResponseMetadata& updated_metadata) override;
52
  void insert(Event::Dispatcher& dispatcher, Key key, Http::ResponseHeaderMapPtr headers,
53
              ResponseMetadata metadata, HttpSourcePtr source,
54
              std::shared_ptr<CacheProgressReceiver> progress) override;
55

            
56
  absl::Mutex mu_;
57
  absl::flat_hash_map<Key, std::shared_ptr<Entry>, MessageUtil, MessageUtil>
58
      entries_ ABSL_GUARDED_BY(mu_);
59
};
60

            
61
} // namespace CacheV2
62
} // namespace HttpFilters
63
} // namespace Extensions
64
} // namespace Envoy