<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using TUMCampusAppAPI.Caches;
using Windows.Storage;

namespace TUMCampusAppAPI.Managers
{
    public class CacheManager : AbstractManager
    {
        //--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
        #region --Attributes--
        public static CacheManager INSTANCE;
        public static readonly int CACHE_TYP_DATA = 0;
        public static readonly int CACHE_TYP_IMAGE = 1;

        /**
         * Validity's for entries in seconds
         */
        public static readonly int VALIDITY_DO_NOT_CACHE = 0;
        public static readonly int VALIDITY_THREE_HOURS = 10800;
        public static readonly int VALIDITY_ONE_DAY = 86400;
        public static readonly int VALIDITY_TWO_DAYS = 2 * 86400;
        public static readonly int VALIDITY_FIFE_DAYS = 5 * 86400;
        public static readonly int VALIDITY_TEN_DAYS = 10 * 86400;
        public static readonly int VALIDITY_ONE_MONTH = 30 * 86400;


        #endregion
        //--------------------------------------------------------Construktor:----------------------------------------------------------------\\
        #region --Construktoren--
        /// <summary>
        /// Basic Constructor
        /// </summary>
        /// <history>
        /// 14/12/2016  Created [<NAME>]
        /// </history>
        public CacheManager()
        {
        }

        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async override Task InitManagerAsync()
        {
            dB.CreateTable<Cache>();
            // Delete all entries that are too old and delete corresponding image files
            foreach (Cache c in dB.Query<Cache>("SELECT * FROM Cache WHERE datetime() > max_age AND type = ?", CACHE_TYP_IMAGE))
            {
                try
                {
                    StorageFile file = await StorageFile.GetFileFromPathAsync(decodeString(c.data));
                    if (file != null)
                    {
                        await file.DeleteAsync();
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("An error occurred during deletion of an old cached file.", e);
                }
            }
            dB.Execute("DELETE FROM Cache WHERE datetime() > max_age");
        }

        public async Task deleteCache()
        {
            try
            {
                StorageFolder folder = (StorageFolder)await ApplicationData.Current.LocalFolder.TryGetItemAsync("Cache");
                if (folder != null)
                {
                    await folder.DeleteAsync(StorageDeleteOption.Default);
                }
                Logger.Info("Deleted Cache folder!");
            }
            catch (Exception e)
            {
                Logger.Error("An error occurred during deletion of a cached file.", e);
            }
            dB.DeleteAll<Cache>();
        }

        /// <summary>
        /// Checks if the given url is cached
        /// </summary>
        /// <param name="url"></param>
        /// <returns>Returns null if it is not chached or the cached string</returns>
        public string isCached(string url)
        {
            List<Cache> list = dB.Query<Cache>("SELECT * FROM Cache WHERE datetime() < max_age AND url LIKE ?", url);
            if(list == null || list.Count <= 0)
            {
                return null;
            }
            return decodeString(list[0].data);
        }

        /// <summary>
        /// Chaches the given Cache object
        /// </summary>
        /// <param name="c"></param>
        public void cache(Cache c)
        {
            dB.InsertOrReplace(c);
        }

        /// <summary>
        /// Decodes a given bytes[] to a string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns>Returns the decoded string</returns>
        public static string decodeString(byte[] bytes)
        {
            return Encoding.UTF8.GetString(bytes);
        }


        /// <summary>
        /// Encodes the given string into a byte[]
        /// </summary>
        /// <param name="s"></param>
        /// <returns>Returns the encode byte[]</returns>
        public static byte[] encodeString(string s)
        {
            return Encoding.UTF8.GetBytes(s);
        }
        #endregion

        #region --Misc Methods (Private)--


        #endregion

        #region --Misc Methods (Protected)--


        #endregion
        //--------------------------------------------------------Events:---------------------------------------------------------------------\\
        #region --Events--


        #endregion
    }
}
<commit_msg>Prevented the app from freezing if the Cache folder / files get deleted NOT A FIX!!<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using TUMCampusAppAPI.Caches;
using Windows.Storage;

namespace TUMCampusAppAPI.Managers
{
    public class CacheManager : AbstractManager
    {
        //--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
        #region --Attributes--
        public static CacheManager INSTANCE;
        public static readonly int CACHE_TYP_DATA = 0;
        public static readonly int CACHE_TYP_IMAGE = 1;

        /**
         * Validity's for entries in seconds
         */
        public static readonly int VALIDITY_DO_NOT_CACHE = 0;
        public static readonly int VALIDITY_THREE_HOURS = 10800;
        public static readonly int VALIDITY_ONE_DAY = 86400;
        public static readonly int VALIDITY_TWO_DAYS = 2 * 86400;
        public static readonly int VALIDITY_FIFE_DAYS = 5 * 86400;
        public static readonly int VALIDITY_TEN_DAYS = 10 * 86400;
        public static readonly int VALIDITY_ONE_MONTH = 30 * 86400;


        #endregion
        //--------------------------------------------------------Construktor:----------------------------------------------------------------\\
        #region --Construktoren--
        /// <summary>
        /// Basic Constructor
        /// </summary>
        /// <history>
        /// 14/12/2016  Created [<NAME>]
        /// </history>
        public CacheManager()
        {
        }

        #endregion
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public async override Task InitManagerAsync()
        {
            dB.CreateTable<Cache>();
            // Delete all entries that are too old and delete corresponding image files
            foreach (Cache c in dB.Query<Cache>("SELECT * FROM Cache WHERE datetime() > max_age AND type = ?", CACHE_TYP_IMAGE))
            {
                try
                {
                    StorageFile file = await StorageFile.GetFileFromPathAsync(decodeString(c.data));
                    if (file != null)
                    {
                        //await file.DeleteAsync(); //TODO Fix App locking up
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("An error occurred during deletion of an old cached file.", e);
                }
            }
            dB.Execute("DELETE FROM Cache WHERE datetime() > max_age");
        }

        public async Task deleteCache()
        {
            try
            {
                StorageFolder folder = (StorageFolder)await ApplicationData.Current.LocalFolder.TryGetItemAsync("Cache");
                if (folder != null)
                {
                    //await folder.DeleteAsync(StorageDeleteOption.Default); //TODO Fix App locking up
                }
                Logger.Info("Deleted Cache folder!");
            }
            catch (Exception e)
            {
                Logger.Error("An error occurred during deletion of a cached file.", e);
            }
            dB.DeleteAll<Cache>();
        }

        /// <summary>
        /// Checks if the given url is cached
        /// </summary>
        /// <param name="url"></param>
        /// <returns>Returns null if it is not chached or the cached string</returns>
        public string isCached(string url)
        {
            List<Cache> list = dB.Query<Cache>("SELECT * FROM Cache WHERE datetime() < max_age AND url LIKE ?", url);
            if(list == null || list.Count <= 0)
            {
                return null;
            }
            return decodeString(list[0].data);
        }

        /// <summary>
        /// Chaches the given Cache object
        /// </summary>
        /// <param name="c"></param>
        public void cache(Cache c)
        {
            dB.InsertOrReplace(c);
        }

        /// <summary>
        /// Decodes a given bytes[] to a string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns>Returns the decoded string</returns>
        public static string decodeString(byte[] bytes)
        {
            return Encoding.UTF8.GetString(bytes);
        }


        /// <summary>
        /// Encodes the given string into a byte[]
        /// </summary>
        /// <param name="s"></param>
        /// <returns>Returns the encode byte[]</returns>
        public static byte[] encodeString(string s)
        {
            return Encoding.UTF8.GetBytes(s);
        }
        #endregion

        #region --Misc Methods (Private)--


        #endregion

        #region --Misc Methods (Protected)--


        #endregion
        //--------------------------------------------------------Events:---------------------------------------------------------------------\\
        #region --Events--


        #endregion
    }
}

{'fraction_non_alphanumeric': 0.20820697498104623, 'fraction_numerical': 0.009666413949962092, 'mean_word_length': 1.1246225085564727, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 71, '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': '3856175', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2388, 'n_words': 686}