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

namespace Kesha.Volatile.Caches
{
    public class ConcurrentDictionaryVolatileCache<TKey, TResult> : IVolatileCache<TKey, TResult>
    {
        private readonly ConcurrentDictionary<TKey, CacheEntry> _items = new ConcurrentDictionary<TKey, CacheEntry>();

        public bool TryGetItem(TKey key, out TResult item)
        {
            CacheEntry cacheEntry;
            if (_items.TryGetValue(key, out cacheEntry))
            {
                if (cacheEntry.Tokens.All(t => t != null && t.IsValid))
                {
                    item = cacheEntry.Result;
                    return true;
                }
            }

            item = default(TResult);
            return false;
        }

        public bool IsItemCached(TKey key)
        {
            TResult item;
            return TryGetItem(key, out item);
        }

        public bool TryRemoveItem(TKey key)
        {
            CacheEntry value;
            return _items.TryRemove(key, out value);
        }

        public void SetItem(TKey key, TResult item, IToken token)
        {
            var cacheEntry = new CacheEntry();
            cacheEntry.Result = item;
            cacheEntry.Attach(token);
            
            _items.AddOrUpdate(key, cacheEntry, (k, i) => cacheEntry);
        }

        public void SetItem(TKey key, Func<TResult> itemFunc, Func<IToken> tokenFunc)
        {
            throw new NotImplementedException();
        }

        private class CacheEntry
        {
            private readonly IList<IToken> _tokens = new List<IToken>();

            public TResult Result { get; set; }

            public IEnumerable<IToken> Tokens
            {
                get
                {
                    return _tokens;
                }
            }

            public void Attach(IToken token)
            {
                _tokens.Add(token);
            }
        }

    }
}<commit_msg>Implemented SetItem method in ConcurrentDictionaryVolatileCache<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace Kesha.Volatile.Caches
{
    public class ConcurrentDictionaryVolatileCache<TKey, TResult> : IVolatileCache<TKey, TResult>
    {
        private readonly ConcurrentDictionary<TKey, CacheEntry> _items = new ConcurrentDictionary<TKey, CacheEntry>();

        public bool TryGetItem(TKey key, out TResult item)
        {
            CacheEntry cacheEntry;
            if (_items.TryGetValue(key, out cacheEntry))
            {
                if (cacheEntry.Tokens.All(token => token != null && token.IsValid))
                {
                    item = cacheEntry.Result;
                    return true;
                }
            }

            item = default(TResult);
            return false;
        }

        public bool IsItemCached(TKey key)
        {
            TResult item;
            return TryGetItem(key, out item);
        }

        public bool TryRemoveItem(TKey key)
        {
            CacheEntry value;
            return _items.TryRemove(key, out value);
        }

        public void SetItem(TKey key, TResult item, IToken token)
        {
            var cacheEntry = CreateCacheEntry(item, token);
            _items.AddOrUpdate(key, cacheEntry, (_, __) => cacheEntry);
        }

        public void SetItem(TKey key, Func<TResult> itemFunc, Func<IToken> tokenFunc)
        {
            _items.AddOrUpdate(
                key,
                _ => CreateCacheEntry(itemFunc(), tokenFunc()),
                (_, __) => CreateCacheEntry(itemFunc(), tokenFunc()));
        }

        private static CacheEntry CreateCacheEntry(TResult item, IToken token)
        {
            var cacheEntry = new CacheEntry();
            cacheEntry.Result = item;
            cacheEntry.Attach(token);
            return cacheEntry;
        }

        private class CacheEntry
        {
            private readonly IList<IToken> _tokens = new List<IToken>();

            public TResult Result { get; set; }

            public IEnumerable<IToken> Tokens
            {
                get { return _tokens; }
            }

            public void Attach(IToken token)
            {
                _tokens.Add(token);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08206410547851785, 'fraction_numerical': 0.0, 'mean_word_length': 1.592810842663524, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '3434883', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1126, 'n_words': 270}