<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoveSeat.Support
{
    internal class TtlDictionary<X, Y> 
    {
        private readonly Dictionary<X, Y> items;
        private readonly Dictionary<X, DateTime> expiration;

        public TtlDictionary()
        {
            items = new Dictionary<X, Y>();
            expiration = new Dictionary<X, DateTime>();
        }

        public void Add(X key, Y value, TimeSpan ttl)
        {
            items.Add(key, value);
            expiration.Add(key, DateTime.Now.Add(ttl));
            RemoveExpiredKeys();
        }

        private void RemoveExpiredKeys()
        {
            foreach (var key in expiration.Keys)
            {
                if (expiration[key] < DateTime.Now)
                {
                    expiration.Remove(key);
                    items.Remove(key);
                }
            }
        }
        public Y this[X key]
        {
            get
            {
                if (expiration.ContainsKey(key) && expiration[key] > DateTime.Now)
                {
                    return items[key];
                }else<commit_msg>avoid error when adding a duplicate key<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoveSeat.Support
{
    internal class TtlDictionary<X, Y> 
    {
        private readonly Dictionary<X, Y> items;
        private readonly Dictionary<X, DateTime> expiration;

        public TtlDictionary()
        {
            items = new Dictionary<X, Y>();
            expiration = new Dictionary<X, DateTime>();
        }

        public void Add(X key, Y value, TimeSpan ttl)
        {
            if (items.ContainsKey(key))
            {
                items.Remove(key);
                expiration.Remove(key);
            }
            items.Add(key, value);
            expiration.Add(key, DateTime.Now.Add(ttl));
            RemoveExpiredKeys();
        }

        private void RemoveExpiredKeys()
        {
            foreach (var key in expiration.Keys)
            {
                if (expiration[key] < DateTime.Now)
                {
                    expiration.Remove(key);
                    items.Remove(key);
                }
            }
        }
        public Y this[X key]
        {
            get
            {
                if (expiration.ContainsKey(key) && expiration[key] > DateTime.Now)
                {
                    return items[key];
                }else
{'fraction_non_alphanumeric': 0.09368874950999607, 'fraction_numerical': 0.0, 'mean_word_length': 1.3585951940850278, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3496769', 'n_tokens_mistral': 672, 'n_tokens_neox': 638, 'n_words': 153}