<commit_before>﻿// Heavily modified version of Postsharp.Cache https://www.nuget.org/packages/PostSharp.Cache

namespace DbSoft.Cache.Aspect
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Caching;

    // Key structure:
    // guid(session).keyname.serialization
    // if no session and no keyname, ..serialization
    // Delete by session: starts with session + '.';
    // Delete by key: contains '.' + keyname + '.';

    public class CacheProvider : ICache
    {
        public static CacheProvider Cache;

        public CacheProvider()
        {
            _cache = MemoryCache.Default;
            _keys = new HashSet<string>();
        }

        public bool Contains(string key)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // return false;
            }
            return _cache[key] != null;
        }

        public void Delete(string key)
        {
            _cache.Remove(key);
            if (_keys.Contains(key))
            {
                _keys.Remove(key);
            }
        }

        public void DeleteAll(string @session)
        {
            ClearAll(session);
        }

        public void DeleteAll()
        {
            ClearAll(null);
        }

        public object this[string key]
        {
            get
            {
                return _cache[key];
            }
            set
            {
                _cache[key] = value;
                if (!_keys.Contains(key))
                {
                    _keys.Add(key);
                }
            }
        }

        private void ClearAll(string searchKey)
        {
            if (string.IsNullOrEmpty(searchKey))
            {
                foreach (var key in _keys)
                {
                    _cache.Remove(key);
                }
                _keys.Clear();
            }
            else
            {
                var keys = _keys.Where(k => k.Contains(searchKey)).ToList();
                foreach (var key in keys)
                {
                    _keys.Remove(key);
                    _cache.Remove(key);
                }
            }
        }
        private readonly ObjectCache _cache;
        private readonly HashSet<string> _keys;
    }<commit_msg>Deleting by session property should be case insensitive<commit_after>﻿// Heavily modified version of Postsharp.Cache https://www.nuget.org/packages/PostSharp.Cache

namespace DbSoft.Cache.Aspect
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Caching;

    // Key structure:
    // guid(session).keyname.serialization
    // if no session and no keyname, ..serialization
    // Delete by session: starts with session + '.';
    // Delete by key: contains '.' + keyname + '.';

    public class CacheProvider : ICache
    {
        public static CacheProvider Cache;

        public CacheProvider()
        {
            _cache = MemoryCache.Default;
            _keys = new HashSet<string>();
        }

        public bool Contains(string key)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // return false;
            }
            return _cache[key] != null;
        }

        public void Delete(string key)
        {
            _cache.Remove(key);
            if (_keys.Contains(key))
            {
                _keys.Remove(key);
            }
        }

        public void DeleteAll(string @session)
        {
            ClearAll(session);
        }

        public void DeleteAll()
        {
            ClearAll(null);
        }

        public object this[string key]
        {
            get
            {
                return _cache[key];
            }
            set
            {
                _cache[key] = value;
                if (!_keys.Contains(key))
                {
                    _keys.Add(key);
                }
            }
        }

        private void ClearAll(string searchKey)
        {
            if (string.IsNullOrEmpty(searchKey))
            {
                foreach (var key in _keys)
                {
                    _cache.Remove(key);
                }
                _keys.Clear();
            }
            else
            {
                var keys = _keys.Where(k => k.IndexOf(searchKey, StringComparison.OrdinalIgnoreCase) >= 0).ToList();
                foreach (var key in keys)
                {
                    _keys.Remove(key);
                    _cache.Remove(key);
                }
            }
        }
        private readonly ObjectCache _cache;
        private readonly HashSet<string> _keys;
    }
{'fraction_non_alphanumeric': 0.09085106382978723, 'fraction_numerical': 0.0002127659574468085, 'mean_word_length': 1.1485374771480805, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7194065', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1175, 'n_words': 261}