<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ReadingList.Logging;
using ReadingList.Logic.Models;
using ReadingList.Logic.Services;
using ReadingList.Trello.Helpers;

namespace ReadingList.Trello.Services
{
    public class CachedReadingListService : IReadingListService
    {
        private readonly IReadingListService _readingListService;
        private readonly ILog _logger;
        private readonly IReadingListCache _readingListCache;

        public CachedReadingListService(
            IReadingListService readingListService,
            IReadingListCache readingListCache,
            ILogFactory logFactory)
        {
            _readingListService = readingListService;
            _logger = logFactory.GetLogger(this.GetType());
            _readingListCache = readingListCache;
        }

        public async Task<bool> AddBookToBacklog(string book, string authors, string label)
        {
            InvalidateCache();
            return await _readingListService.AddBookToBacklog(book, authors, label);
        }

        public async Task<IEnumerable<Book>> GetReadingList(string listName, string label = null)
        {
            IEnumerable<Book> books;
            if (_readingListCache.TryGetValue(new KeyValuePair<string, string>(listName, label), out books))
            {
                return !string.IsNullOrEmpty(label) ? books.Where(b => b.Label.ToString().ToLower().Equals(label.ToLower())) : books;
            }

            _logger.Info($"Cache miss for {listName}, {label}");
            books = await _readingListService.GetReadingList(listName, label);
            _readingListCache.TryAdd(new KeyValuePair<string, string>(listName, label), books);
            return books;

        }
<commit_msg>Splitting lists from and to cache to its own instances (#136)<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ReadingList.Logging;
using ReadingList.Logic.Models;
using ReadingList.Logic.Services;
using ReadingList.Trello.Helpers;

namespace ReadingList.Trello.Services
{
    public class CachedReadingListService : IReadingListService
    {
        private readonly IReadingListService _readingListService;
        private readonly ILog _logger;
        private readonly IReadingListCache _readingListCache;

        public CachedReadingListService(
            IReadingListService readingListService,
            IReadingListCache readingListCache,
            ILogFactory logFactory)
        {
            _readingListService = readingListService;
            _logger = logFactory.GetLogger(this.GetType());
            _readingListCache = readingListCache;
        }

        public async Task<bool> AddBookToBacklog(string book, string authors, string label)
        {
            InvalidateCache();
            return await _readingListService.AddBookToBacklog(book, authors, label);
        }

        public async Task<IEnumerable<Book>> GetReadingList(string listName, string label = null)
        {
            IEnumerable<Book> cachedBooks;
            if (_readingListCache.TryGetValue(new KeyValuePair<string, string>(listName, label), out cachedBooks))
            {
                return !string.IsNullOrEmpty(label) ? cachedBooks.Where(b => b.Label.ToString().ToLower().Equals(label.ToLower())) : cachedBooks;
            }
            
            _logger.Info($"Cache miss for {listName}, {label}");
            var booksFromService = await _readingListService.GetReadingList(listName, label);
            _readingListCache.TryAdd(new KeyValuePair<string, string>(listName, label), booksFromService);
            return booksFromService;

        }

{'fraction_non_alphanumeric': 0.08026845637583893, 'fraction_numerical': 0.0008053691275167785, 'mean_word_length': 2.9138655462184873, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '1458165', 'n_tokens_mistral': 986, 'n_tokens_neox': 943, 'n_words': 209}