<commit_before>package etcdcache

import (
	"context"
	"errors"
	"sync"

	etcd "github.com/coreos/etcd/clientv3"
	"github.com/coreos/etcd/mvcc/mvccpb"
	"github.com/innovate-technologies/Dispatch/dispatchd/etcdclient"
)

var etcdAPI = etcdclient.GetEtcdv3()

// EtcdCache stores etcd records in memory to do requests on
type EtcdCache struct {
	cache      map[string]*mvccpb.KeyValue
	cacheMutex sync.Mutex
}

// New gives a new empty EtcdCache
func New() *EtcdCache {
	return &EtcdCache{
		cache:      map[string]*mvccpb.KeyValue{},
		cacheMutex: sync.Mutex{},
	}
}

// NewForPrefix  returns a new EtcdCache prefilled with the keys of a specific prefix
func NewForPrefix(prefix string) (*EtcdCache, error) {
	cache := New()
	res, err := etcdAPI.Get(context.Background(), prefix, etcd.WithPrefix())
	if err != nil {
		return nil, err
	}
	for _, kv := range res.Kvs {
		cache.Put(kv)
	}

	return cache, nil
}

// Get gets a key out of the cache
func (e *EtcdCache) Get(key string) (*mvccpb.KeyValue, error) {
	e.cacheMutex.Lock()
	defer e.cacheMutex.Unlock()
	if val, ok := e.cache[key]; ok {
		return val, nil
	}
	return nil, errors.New("Key not in cache")
}

// Put allows to add a new record
func (e *EtcdCache) Put(val *mvccpb.KeyValue) {
	e.cacheMutex.Lock()
	e.cache[string(val.Key)] = val<commit_msg>send empty cache in case of error<commit_after>package etcdcache

import (
	"context"
	"errors"
	"sync"

	etcd "github.com/coreos/etcd/clientv3"
	"github.com/coreos/etcd/mvcc/mvccpb"
	"github.com/innovate-technologies/Dispatch/dispatchd/etcdclient"
)

var etcdAPI = etcdclient.GetEtcdv3()

// EtcdCache stores etcd records in memory to do requests on
type EtcdCache struct {
	cache      map[string]*mvccpb.KeyValue
	cacheMutex sync.Mutex
}

// New gives a new empty EtcdCache
func New() *EtcdCache {
	return &EtcdCache{
		cache:      map[string]*mvccpb.KeyValue{},
		cacheMutex: sync.Mutex{},
	}
}

// NewForPrefix  returns a new EtcdCache prefilled with the keys of a specific prefix
func NewForPrefix(prefix string) (*EtcdCache, error) {
	cache := New()
	res, err := etcdAPI.Get(context.Background(), prefix, etcd.WithPrefix())
	if err != nil {
		return cache, err
	}
	for _, kv := range res.Kvs {
		cache.Put(kv)
	}

	return cache, nil
}

// Get gets a key out of the cache
func (e *EtcdCache) Get(key string) (*mvccpb.KeyValue, error) {
	e.cacheMutex.Lock()
	defer e.cacheMutex.Unlock()
	if val, ok := e.cache[key]; ok {
		return val, nil
	}
	return nil, errors.New("Key not in cache")
}

// Put allows to add a new record
func (e *EtcdCache) Put(val *mvccpb.KeyValue) {
	e.cacheMutex.Lock()
	e.cache[string(val.Key)] = val
{'fraction_non_alphanumeric': 0.12917933130699089, 'fraction_numerical': 0.001519756838905775, 'mean_word_length': 3.958568738229755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4801201', 'n_tokens_mistral': 995, 'n_tokens_neox': 982, 'n_words': 267}