<commit_before>﻿// Copyright (c) <NAME> 2014
// See License.txt file for more details

using System;
using ThinMvvm.WindowsRuntime.Internals;
using Windows.Storage;

namespace ThinMvvm.WindowsRuntime
{
    /// <summary>
    /// <see cref="IDataCache" /> implementation for the Windows Runtime.
    /// </summary>
    public sealed class WindowsRuntimeDataCache : IDataCache
    {
        // N.B.: CreateContainer(string, ApplicationDataCreateDisposition) will throw 
        //       if it doesn't exist when using Existing, so we have to use Always instead.

        private const string DateContainerSuffix = "#Date";

        private readonly ApplicationDataContainer _settings = ApplicationData.Current.LocalSettings;


        /// <summary>
        /// Attempts to get the value stored by the specified owner type, with the specified ID.
        /// </summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="owner">The owner type.</param>
        /// <param name="id">The ID.</param>
        /// <param name="value">The value, if any.</param>
        /// <returns>A value indicating whether a value was found.</returns>
        public bool TryGet<T>( Type owner, long id, out T value )
        {
            string key = id.ToString();
            var container = _settings.CreateContainer( owner.FullName, ApplicationDataCreateDisposition.Always );
            if ( !container.Values.ContainsKey( key ) )
            {
                value = default( T );
                return false;
            }

            var dateContainer = _settings.CreateContainer( owner.FullName + DateContainerSuffix, ApplicationDataCreateDisposition.Always );
            if ( (DateTimeOffset) dateContainer.Values[key] < DateTimeOffset.UtcNow )
            {
                container.Values.Remove( key );
                dateContainer.Values.Remove( key );

                if ( container.Values.Count == 0 )
                {
                    _settings.DeleteContainer( container.Name );
                    _settings.DeleteContainer( dateContainer.Name );
                }

                value = default( T );
                return false;
            }

            value = Serializer.Deserialize<T>( (string) container.Values[key] );
            return true;
        }

        /// <summary>
        /// Sets the specified value for the specified owner type, with the specified ID.
        /// </summary>
        /// <param name="owner">The owner type.</param>
        /// <param name="id">The ID.</param>
        /// <param name="expirationDate">The expiration date.</param>
        /// <param name="value">The value.</param>
        public void Set( Type owner, long id, DateTimeOffset expirationDate, object value )
        {
            var typeContainer = _settings.CreateContainer( owner.FullName, ApplicationDataCreateDisposition.Always );
            var dateContainer = _settings.CreateContainer( owner.FullName + DateContainerSuffix, ApplicationDataCreateDisposition.Always );
            string key = id.ToString();

            typeContainer.Values[key] = Serializer.Serialize( value );
            dateContainer.Values[key] = expirationDate;
        }
    }
}<commit_msg>Fail fast instead of giving users a cryptic WinRT message in WindowsRuntimeDataCache.<commit_after>﻿// Copyright (c) <NAME> 2014
// See License.txt file for more details

using System;
using ThinMvvm.WindowsRuntime.Internals;
using Windows.Storage;

namespace ThinMvvm.WindowsRuntime
{
    /// <summary>
    /// <see cref="IDataCache" /> implementation for the Windows Runtime.
    /// </summary>
    public sealed class WindowsRuntimeDataCache : IDataCache
    {
        // N.B.: CreateContainer(string, ApplicationDataCreateDisposition) will throw 
        //       if it doesn't exist when using Existing, so we have to use Always instead.

        private const string DateContainerSuffix = "#Date";

        private readonly ApplicationDataContainer _settings = ApplicationData.Current.LocalSettings;


        /// <summary>
        /// Attempts to get the value stored by the specified owner type, with the specified ID.
        /// </summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="owner">The owner type.</param>
        /// <param name="id">The ID.</param>
        /// <param name="value">The value, if any.</param>
        /// <returns>A value indicating whether a value was found.</returns>
        public bool TryGet<T>( Type owner, long id, out T value )
        {
            string key = id.ToString();
            var container = _settings.CreateContainer( owner.FullName, ApplicationDataCreateDisposition.Always );
            if ( !container.Values.ContainsKey( key ) )
            {
                value = default( T );
                return false;
            }

            var dateContainer = _settings.CreateContainer( owner.FullName + DateContainerSuffix, ApplicationDataCreateDisposition.Always );
            if ( (DateTimeOffset) dateContainer.Values[key] < DateTimeOffset.UtcNow )
            {
                container.Values.Remove( key );
                dateContainer.Values.Remove( key );

                if ( container.Values.Count == 0 )
                {
                    _settings.DeleteContainer( container.Name );
                    _settings.DeleteContainer( dateContainer.Name );
                }

                value = default( T );
                return false;
            }

            value = Serializer.Deserialize<T>( (string) container.Values[key] );
            return true;
        }

        /// <summary>
        /// Sets the specified value for the specified owner type, with the specified ID.
        /// </summary>
        /// <param name="owner">The owner type.</param>
        /// <param name="id">The ID.</param>
        /// <param name="expirationDate">The expiration date.</param>
        /// <param name="value">The value.</param>
        public void Set( Type owner, long id, DateTimeOffset expirationDate, object value )
        {
            // HACK
            if ( expirationDate == DateTimeOffset.MaxValue )
            {
                throw new InvalidOperationException(
                    "DateTimeOffset.MaxValue cannot be used as the expiration date on the Windows Runtime because of a Windows Runtime bug."
                  + Environment.NewLine
                  + "Please use new DateTimeOffset( 9999, 12, 31, 00, 00, 00, TimeSpan.Zero ) instead." );
            }

            var typeContainer = _settings.CreateContainer( owner.FullName, ApplicationDataCreateDisposition.Always );
            var dateContainer = _settings.CreateContainer( owner.FullName + DateContainerSuffix, ApplicationDataCreateDisposition.Always );
            string key = id.ToString();

            typeContainer.Values[key] = Serializer.Serialize( value );
            dateContainer.Values[key] = expirationDate;
        }
    }
}
{'fraction_non_alphanumeric': 0.10474960539532215, 'fraction_numerical': 0.0034438226431338786, 'mean_word_length': 2.062390158172232, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '409021', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1649, 'n_words': 490}