Skip to content

Commit

Permalink
Add multi-targeting for getting cache locations
Browse files Browse the repository at this point in the history
Also updated tests
Added optional, but recommended UniqueId
  • Loading branch information
jamesmontemagno committed Dec 13, 2017
1 parent 736dc6e commit 757d163
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/MonkeyCache.Tests/BarrelTests.cs
Expand Up @@ -21,6 +21,7 @@ public class BarrelTests
[TestInitialize]
public void Setup()
{
Barrel.UniqueId = "com.refractored.monkeycache";
url = "http://montemagno.com/monkeys.json";
barrel = Barrel.Current;

Expand Down
1 change: 1 addition & 0 deletions src/MonkeyCache.Tests/HttpCacheTests.cs
Expand Up @@ -17,6 +17,7 @@ public class HttpCacheTests
[TestInitialize]
public void Setup()
{
Barrel.UniqueId = "com.refractored.monkeycache";
url = "http://montemagno.com/monkeys.json";
barrel = Barrel.Current;

Expand Down
39 changes: 28 additions & 11 deletions src/MonkeyCache/Barrel.cs
Expand Up @@ -4,6 +4,11 @@
using SQLite;
using Newtonsoft.Json;
using System.Reflection;
#if __IOS__ || __MACOS__
using Foundation;
#elif __ANDROID__
using Android.App;
#endif

namespace MonkeyCache
{
Expand All @@ -13,12 +18,24 @@ namespace MonkeyCache
/// </summary>
public class Barrel
{
public static string UniqueId { get; set; } = string.Empty;

static readonly Lazy<string> baseCacheDir = new Lazy<string>(() =>
{
var path = string.Empty;
///Gets full path based on device type.
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if (AppDomain.CurrentDomain.GetAssemblies().Any(x => x.GetName().Name == "Xamarin.iOS"))
path = Path.GetFullPath(Path.Combine(path, "..", "Library", "Databases"));
#if __IOS__ || __MACOS__
path = NSSearchPath.GetDirectories(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.User)[0];
#elif __ANDROID__
path = Application.Context.CacheDir.AbsolutePath;
#elif __UWP__
path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#else
path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
#endif
if (!string.IsNullOrWhiteSpace(UniqueId))
path = Path.Combine(path, UniqueId);
return Path.Combine(path, "MonkeyCache");
});
Expand Down Expand Up @@ -54,7 +71,7 @@ public class Barrel
};
}

#region Exist and Expiration Methods
#region Exist and Expiration Methods
/// <summary>
/// Checks to see if the key exists in the Barrel.
/// </summary>
Expand Down Expand Up @@ -90,9 +107,9 @@ public bool IsExpired(string key)
return DateTime.UtcNow > ent.ExpirationDate;
}

#endregion
#endregion

#region Get Methods
#region Get Methods

/// <summary>
/// Gets the data entry for the specified key.
Expand Down Expand Up @@ -151,9 +168,9 @@ public string GetETag(string key)
return ent.ETag;
}

#endregion
#endregion

#region Add Methods
#region Add Methods

/// <summary>
/// Adds a string netry to the barrel
Expand Down Expand Up @@ -208,9 +225,9 @@ public void Add<T>(string key, T data, TimeSpan expireIn, string eTag = null)
}
}

#endregion
#endregion

#region Empty Methods
#region Empty Methods
/// <summary>
/// Empties all expired entries that are in the Barrel.
/// Throws an exception if any deletions fail and rolls back changes.
Expand Down Expand Up @@ -260,6 +277,6 @@ public void Empty(params string[] key)
}
}

#endregion
#endregion
}
}
67 changes: 66 additions & 1 deletion src/MonkeyCache/MonkeyCache.csproj
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;MonoAndroid50;Xamarin.Mac20;uap10.0</TargetFrameworks>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyFileVersion>1.0.0.0</AssemblyFileVersion>
<Version>1.0.0.0</Version>
Expand All @@ -19,6 +20,41 @@
<Title>MonkeyCache</Title>
<Description>The best cache that is always monkeying around with your data.</Description>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0'">
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<NugetTargetMoniker>UAP,Version=v10.0</NugetTargetMoniker>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.16299.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<TargetFrameworkIdentifier>.NETCore</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
<DefineConstants>$(DefineConstants);__UWP__</DefineConstants>
<LanguageTargets>$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets</LanguageTargets>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'Xamarin.iOS10'">
<TargetFrameworkIdentifier>Xamarin.iOS</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
<DefineConstants>$(DefineConstants);__IOS__</DefineConstants>
<DebugType>full</DebugType>
<LanguageTargets>$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets</LanguageTargets>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'Xamarin.Mac20'">
<TargetFrameworkIdentifier>Xamarin.Mac</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<DefineConstants>$(DefineConstants);__MACOS__</DefineConstants>
<DebugType>full</DebugType>
<LanguageTargets>$(MSBuildExtensionsPath)\Xamarin\Mac\Xamarin.Mac.CSharp.targets</LanguageTargets>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'MonoAndroid50'">
<TargetFrameworkIdentifier>MonoAndroid</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
<DebugType>full</DebugType>
<DefineConstants>$(DefineConstants);__ANDROID__</DefineConstants>
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<LanguageTargets>$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets</LanguageTargets>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>latest</LangVersion>
Expand All @@ -29,6 +65,35 @@
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'Xamarin.iOS10' ">
<Reference Include="Xamarin.iOS" />
<Reference Include="System" />
<Reference Include="System.Collections" />
<Reference Include="System.Core" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'Xamarin.Mac20' ">
<Reference Include="Xamarin.Mac" />
<Reference Include="System" />
<Reference Include="System.Collections" />
<Reference Include="System.Core" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'MonoAndroid50' ">
<Reference Include="Mono.Android" />
<Reference Include="System" />
<Reference Include="System.Collections" />
<Reference Include="System.Core" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'uap10.0' ">
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform " Version="6.0.1" />

</ItemGroup>

<ItemGroup>
<PackageReference Include="sqlite-net-pcl" Version="1.4.118" />
Expand Down

0 comments on commit 757d163

Please sign in to comment.