github.com/ledgerwatch/erigon-lib@v1.0.0/kv/kvcache/dummy.go (about)

     1  /*
     2  Copyright 2021 Erigon contributors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package kvcache
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
    22  	"github.com/ledgerwatch/erigon-lib/kv"
    23  )
    24  
    25  // DummyCache - doesn't remember anything - can be used when service is not remote
    26  type DummyCache struct{}
    27  
    28  var _ Cache = (*DummyCache)(nil)    // compile-time interface check
    29  var _ CacheView = (*DummyView)(nil) // compile-time interface check
    30  
    31  func NewDummy() *DummyCache { return &DummyCache{} }
    32  func (c *DummyCache) View(_ context.Context, tx kv.Tx) (CacheView, error) {
    33  	return &DummyView{cache: c, tx: tx}, nil
    34  }
    35  func (c *DummyCache) OnNewBlock(sc *remote.StateChangeBatch) {}
    36  func (c *DummyCache) Evict() int                             { return 0 }
    37  func (c *DummyCache) Len() int                               { return 0 }
    38  func (c *DummyCache) Get(k []byte, tx kv.Tx, id uint64) ([]byte, error) {
    39  	return tx.GetOne(kv.PlainState, k)
    40  }
    41  func (c *DummyCache) GetCode(k []byte, tx kv.Tx, id uint64) ([]byte, error) {
    42  	return tx.GetOne(kv.Code, k)
    43  }
    44  func (c *DummyCache) ValidateCurrentRoot(_ context.Context, _ kv.Tx) (*CacheValidationResult, error) {
    45  	return &CacheValidationResult{Enabled: false}, nil
    46  }
    47  
    48  type DummyView struct {
    49  	cache *DummyCache
    50  	tx    kv.Tx
    51  }
    52  
    53  func (c *DummyView) Get(k []byte) ([]byte, error)     { return c.cache.Get(k, c.tx, 0) }
    54  func (c *DummyView) GetCode(k []byte) ([]byte, error) { return c.cache.GetCode(k, c.tx, 0) }