<commit_before>_ = require 'underscore'

class Cache

  constructor: (opts = {}) ->
    { @max_items } = opts
    unless @max_memory || @max_items
      throw new Error "Must specify some kind of size limit"
    @reset()

  reset: ->
    @sticky = {}
    @map = {}
    @head = new CacheItem
    @head.next = @head.prev = @head
    @stats =
      lru_items: 0
      sticky_items: 0
      lru_hits: 0
      sticky_hits: 0
      misses: 0
      added: 0
      removed: 0

  add: (key, value, sticky = false) ->
    return if @map[key] != undefined
    return if @sticky[key] != undefined
    item = new CacheItem key, value
    if sticky
      @stats.sticky_items++
      @sticky[key] = item
      return item.get()
    @shrink()
    item.add_after @head
    @stats.lru_items++
    @stats.added++
    @map[key] = item
    item.get()

  remove: (key) ->
    if item = @map[key]
      delete @map[key]
      item.remove()
      @stats.lru_items--
      @stats.removed++
      item.value

  get: (key) ->
    if (item = @sticky[key]) != undefined
      @stats.sticky_hits++
      item.get()
    else if item = @map[key]<commit_msg>Fix in cache<commit_after>_ = require 'underscore'

class Cache

  constructor: (opts = {}) ->
    { @max_items } = opts
    unless @max_memory || @max_items
      throw new Error "Must specify some kind of size limit"
    @reset()

  reset: ->
    @sticky = {}
    @map = {}
    @head = new CacheItem
    @head.next = @head.prev = @head
    @stats =
      lru_items: 0
      sticky_items: 0
      lru_hits: 0
      sticky_hits: 0
      misses: 0
      added: 0
      removed: 0

  add: (key, value, sticky = false) ->
    return item.get() if item = (@sticky[key] || @map[key])
    item = new CacheItem key, value
    if sticky
      @stats.sticky_items++
      @sticky[key] = item
      return item.get()
    @shrink()
    item.add_after @head
    @stats.lru_items++
    @stats.added++
    @map[key] = item
    item.get()

  remove: (key) ->
    if item = @map[key]
      delete @map[key]
      item.remove()
      @stats.lru_items--
      @stats.removed++
      item.value

  get: (key) ->
    if (item = @sticky[key]) != undefined
      @stats.sticky_hits++
      item.get()
    else if item = @map[key]
{'fraction_non_alphanumeric': 0.14068100358422939, 'fraction_numerical': 0.0062724014336917565, 'mean_word_length': 2.1013888888888888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '181753', 'n_tokens_mistral': 819, 'n_tokens_neox': 792, 'n_words': 205}