<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using StockportContentApi.Model;

namespace StockportContentApi.Utils
{
    public interface IDistributedCacheWrapper
    {
        void Remove(string key);
        void SetString(string key, string value, int minutes);
        string GetString(string key);
        List<RedisValueData> GetKeys();
    }

    public class DistributedCacheWrapper : IDistributedCacheWrapper
    {
        private readonly ConnectionMultiplexer _cache;
        private readonly IServer server;
        private readonly IDatabase db;


        public DistributedCacheWrapper(ConnectionMultiplexer cache)
        {
            _cache = cache;
            server = _cache.GetServer(_cache.Configuration);
            db = _cache.GetDatabase();
        }

        public string GetString(string key)
        {
            return db.HashGet(key, "ContentApiData");
        }

        public void Remove(string key)
        {
            db.KeyDelete(key);
        }

        public void SetString(string key, string value, int minutes)
        {
            db.HashSet(key, "ContentApiData", value);
            db.KeyExpire(key, DateTime.Now.AddMinutes(minutes));
        }

        public List<RedisValueData> GetKeys()
        {
            var keys = server.Keys();

            var redisValueData = new List<RedisValueData>();
            keys.ToList().ForEach(k =>
            {
                var valueWithExpiry = db.StringGetWithExpiry(k);

                var data = new RedisValueData
                {
                    NumberOfItems = 1,
                    Expiry = valueWithExpiry.Expiry.ToString(),
                    Key = k.ToString()
                };

                var jsonData = JsonConvert.DeserializeObject(valueWithExpiry.Value.ToString()) as JArray;

                if(jsonData != null) data.NumberOfItems = jsonData.Count;

                redisValueData.Add(data);
            });
            return redisValueData;
        }
    }
}<commit_msg>David/Stephen #1203 - Reverting back to storing as strings and the GetAllKeys filters strings<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using StockportContentApi.Model;

namespace StockportContentApi.Utils
{
    public interface IDistributedCacheWrapper
    {
        void Remove(string key);
        void SetString(string key, string value, int minutes);
        string GetString(string key);
        List<RedisValueData> GetKeys();
    }

    public class DistributedCacheWrapper : IDistributedCacheWrapper
    {
        private readonly ConnectionMultiplexer _cache;
        private readonly IServer server;
        private readonly IDatabase db;


        public DistributedCacheWrapper(ConnectionMultiplexer cache)
        {
            _cache = cache;
            server = _cache.GetServer(_cache.Configuration);
            db = _cache.GetDatabase();
        }

        public string GetString(string key)
        {
            return db.StringGet(key);
        }

        public void Remove(string key)
        {
            db.KeyDelete(key);
        }

        public void SetString(string key, string value, int minutes)
        {
            db.StringAppend(key, value);
            db.KeyExpire(key, DateTime.Now.AddMinutes(minutes));
        }

        public List<RedisValueData> GetKeys()
        {
            var keys = server.Keys();

            var redisValueData = new List<RedisValueData>();
            keys.Where(k => db.KeyType(k) == RedisType.String).ToList().ForEach(k =>
            {
                var valueWithExpiry = db.StringGetWithExpiry(k);

                var data = new RedisValueData
                {
                    NumberOfItems = 1,
                    Expiry = valueWithExpiry.Expiry.ToString(),
                    Key = k.ToString()
                };

                var jsonData = JsonConvert.DeserializeObject(valueWithExpiry.Value.ToString()) as JArray;

                if (jsonData != null) data.NumberOfItems = jsonData.Count;

                redisValueData.Add(data);
            });
            return redisValueData;
        }
    }
}
{'fraction_non_alphanumeric': 0.08306414397784956, 'fraction_numerical': 0.0013844023996308261, 'mean_word_length': 1.940976933514247, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, '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': '1084042', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1110, 'n_words': 253}