<commit_before>package lazycache
// A generic cache that favors returning stale data
// than blocking a caller

import (
  "sync"
  "time"
)

type Fetcher func(id string) (interface{}, error)

type Item struct {
  object interface{}
  expires time.Time
}

type LazyCache struct {
  fetcher Fetcher
  ttl time.Duration
  lock sync.RWMutex
  items map[string]*Item
}

func New(fetcher Fetcher, ttl time.Duration, size int) *LazyCache {
  return &LazyCache{
    ttl: ttl,
    fetcher: fetcher,
    items: make(map[string]*Item, size),
  }
}

func (cache *LazyCache) Get(id string) (interface{}, bool) {
  cache.lock.RLock()
  item, exists := cache.items[id]
  cache.lock.RUnlock()
  if exists == false {
    return cache.Fetch(id, item)
  }
  if time.Now().After(item.expires) {
    if item.object == nil {
      return cache.Fetch(id, item)
    } else {
      go cache.Fetch(id, item)
    }
  }
  return item.object, true
}

func (cache *LazyCache) Set(id string, object interface{}) {
  cache.lock.Lock()
  defer cache.lock.Unlock()
  cache.items[id] = &Item{expires: time.Now().Add(cache.ttl), object: object}
}

func (cache *LazyCache) Fetch(id string, current *Item) (interface{}, bool) {
  object, err := cache.fetcher(id)
  if err != nil { return nil, false }
  
  if current == nil {
    if object == nil { return nil, false }
    cache.Set(id, object)
    return object, true
  }
<commit_msg>shoul set misses<commit_after>package lazycache
// A generic cache that favors returning stale data
// than blocking a caller

import (
  "sync"
  "time"
)

type Fetcher func(id string) (interface{}, error)

type Item struct {
  object interface{}
  expires time.Time
}

type LazyCache struct {
  fetcher Fetcher
  ttl time.Duration
  lock sync.RWMutex
  items map[string]*Item
}

func New(fetcher Fetcher, ttl time.Duration, size int) *LazyCache {
  return &LazyCache{
    ttl: ttl,
    fetcher: fetcher,
    items: make(map[string]*Item, size),
  }
}

func (cache *LazyCache) Get(id string) (interface{}, bool) {
  cache.lock.RLock()
  item, exists := cache.items[id]
  cache.lock.RUnlock()
  if exists == false {
    return cache.Fetch(id, item)
  }
  if time.Now().After(item.expires) {
    if item.object == nil {
      return cache.Fetch(id, item)
    } else {
      go cache.Fetch(id, item)
    }
  }
  return item.object, true
}

func (cache *LazyCache) Set(id string, object interface{}) {
  cache.lock.Lock()
  defer cache.lock.Unlock()
  cache.items[id] = &Item{expires: time.Now().Add(cache.ttl), object: object}
}

func (cache *LazyCache) Fetch(id string, current *Item) (interface{}, bool) {
  object, err := cache.fetcher(id)
  if err != nil { return nil, false }
  
  if current == nil {
    cache.Set(id, object)
    return object, object != nil
  }

{'fraction_non_alphanumeric': 0.1349005424954792, 'fraction_numerical': 0.0, 'mean_word_length': 3.3765822784810124, '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': '6363125', 'n_tokens_mistral': 971, 'n_tokens_neox': 967, 'n_words': 273}