@@ -19,6 +19,7 @@ typedef struct {
19
19
int enable_fifo;
20
20
int enable_lru;
21
21
int enable_block;
22
+ int enable_block_v2;
22
23
} config;
23
24
24
25
static config g_cfg;
@@ -662,9 +663,9 @@ class CacheStatImpl : public CacheStat {
662
663
long last_miss_length_ = 0 ;
663
664
};
664
665
665
- class LRUCache : public Cache , public CacheStatImpl {
666
+ template < typename Impl> class TCache : public Cache , public CacheStatImpl {
666
667
public:
667
- LRUCache (size_t cap)
668
+ TCache (size_t cap)
668
669
: impl_(cap)
669
670
{
670
671
}
@@ -690,70 +691,13 @@ class LRUCache : public Cache, public CacheStatImpl {
690
691
}
691
692
692
693
private:
693
- LRUCacheImpl<string> impl_;
694
+ Impl impl_;
694
695
};
695
696
696
- class FIFOCache : public Cache , public CacheStatImpl {
697
- public:
698
- FIFOCache (size_t cap)
699
- : impl_(cap)
700
- {
701
- }
702
-
703
- void get (const string &key, const size_t length) override
704
- {
705
- auto [is_hit, old_length] = impl_.get (key);
706
- if (!is_hit || old_length != length) {
707
- is_hit = false ;
708
- impl_.set (key, length);
709
- }
710
- on_get (key, length, is_hit);
711
- }
712
-
713
- Stat get_stat () const override
714
- {
715
- Stat stat;
716
- stat.class_name = get_filt_type_name<decltype (*this )>();
717
- stat.capacity = impl_.capacity ();
718
- stat.count = impl_.count ();
719
- stat.used = impl_.used ();
720
- return stat;
721
- }
722
-
723
- private:
724
- FIFOCacheImpl<string> impl_;
725
- };
726
-
727
- class BlockCache : public Cache , public CacheStatImpl {
728
- public:
729
- BlockCache (size_t cap)
730
- : impl_(cap)
731
- {
732
- }
733
-
734
- void get (const string &key, const size_t length) override
735
- {
736
- auto [is_hit, old_length] = impl_.get (key);
737
- if (!is_hit || old_length != length) {
738
- is_hit = false ;
739
- impl_.set (key, length);
740
- }
741
- on_get (key, length, is_hit);
742
- }
743
-
744
- Stat get_stat () const override
745
- {
746
- Stat stat;
747
- stat.class_name = get_filt_type_name<decltype (*this )>();
748
- stat.capacity = impl_.capacity ();
749
- stat.count = impl_.count ();
750
- stat.used = impl_.used ();
751
- return stat;
752
- }
753
-
754
- private:
755
- BlockCacheImpl<string> impl_;
756
- };
697
+ using LRUCache = TCache<LRUCacheImpl<string>>;
698
+ using FIFOCache = TCache<FIFOCacheImpl<string>>;
699
+ using BlockCache = TCache<BlockCacheImpl<string>>;
700
+ using BlockCacheV2 = TCache<BlockCacheImplV2<string>>;
757
701
758
702
vector<string> split (const string &str, const string &delim)
759
703
{
@@ -774,6 +718,7 @@ vector<string> split(const string &str, const string &delim)
774
718
LRUCache *g_lru_cache = nullptr ;
775
719
FIFOCache *g_fifo_cache = nullptr ;
776
720
BlockCache *g_block_cache = nullptr ;
721
+ BlockCacheV2 *g_block_cache_v2 = nullptr ;
777
722
778
723
static void cache_get (const string &key, const long len)
779
724
{
@@ -790,6 +735,10 @@ static void cache_get(const string &key, const long len)
790
735
if (g_cfg.enable_block ) {
791
736
g_block_cache->get (key, len);
792
737
}
738
+
739
+ if (g_cfg.enable_block_v2 ) {
740
+ g_block_cache_v2->get (key, len);
741
+ }
793
742
}
794
743
795
744
static void process_line (const string &line)
@@ -855,6 +804,7 @@ static command_t cmds[] = { { "c", "cap", cmd_set_size, offsetof(config, capacit
855
804
{ " " , " fifo" , cmd_set_bool, offsetof (config, enable_fifo), " on" },
856
805
{ " " , " lru" , cmd_set_bool, offsetof (config, enable_lru), " on" },
857
806
{ " " , " block" , cmd_set_bool, offsetof (config, enable_block), " on" },
807
+ { " " , " block_v2" , cmd_set_bool, offsetof (config, enable_block_v2), " on" },
858
808
{ " " , " v2" , nullptr , offsetof (config, is_v2), nullptr , " " } };
859
809
860
810
int main (int argc, const char *argv[])
@@ -869,6 +819,7 @@ int main(int argc, const char *argv[])
869
819
g_lru_cache = new LRUCache (g_cfg.capacity );
870
820
g_fifo_cache = new FIFOCache (g_cfg.capacity );
871
821
g_block_cache = new BlockCache (g_cfg.capacity );
822
+ g_block_cache_v2 = new BlockCacheV2 (g_cfg.capacity );
872
823
873
824
for (string line; std::getline (std::cin, line);) {
874
825
if (g_cfg.is_v2 ) {
0 commit comments