Skip to content

Commit

Permalink
Start to refactor so that paths can be configurable per-platform via …
Browse files Browse the repository at this point in the history
…Service Location
  • Loading branch information
anaisbetts committed Mar 14, 2013
1 parent 11f8b45 commit 2efe6a3
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 84 deletions.
39 changes: 27 additions & 12 deletions Akavache/Akavache_Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\Akavache\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -32,20 +33,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.4.5.3\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="ReactiveUI">
<HintPath>..\packages\reactiveui-core.3.1.0\lib\Net4\ReactiveUI.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Reactive">
<HintPath>..\..\ReactiveUI\ext\System.Reactive.dll</HintPath>
</Reference>
<Reference Include="System.Security" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -55,6 +44,27 @@
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="Mono.Posix" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\ext\mono\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="ReactiveUI">
<HintPath>..\ext\mono\ReactiveUI.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Core">
<HintPath>..\ext\mono\System.Reactive.Core.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Interfaces">
<HintPath>..\ext\mono\System.Reactive.Interfaces.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Linq">
<HintPath>..\ext\mono\System.Reactive.Linq.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.PlatformServices">
<HintPath>..\ext\mono\System.Reactive.PlatformServices.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Windows.Threading">
<HintPath>..\ext\mono\System.Reactive.Windows.Threading.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BlobCache.cs" />
Expand All @@ -70,6 +80,11 @@
<Compile Include="StreamMixins.cs" />
<Compile Include="TestBlobCache.cs" />
<Compile Include="Utility.cs" />
<Compile Include="JsonObjectConverter.cs" />
<Compile Include="IObjectCreator.cs" />
<Compile Include="MD5.cs" />
<Compile Include="MD5Managed.cs" />
<Compile Include="IsolatedStorageProvider.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
6 changes: 6 additions & 0 deletions Akavache/BlobCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

namespace Akavache
{
// Ignore this man behind the curtain
internal interface IWantsToRegisterStuff
{
void Register();
}

public static class BlobCache
{
static IServiceProvider serviceProvider;
Expand Down
27 changes: 11 additions & 16 deletions Akavache/EncryptedBlobCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ namespace Akavache
{
public abstract class EncryptedBlobCache : PersistentBlobCache, ISecureBlobCache
{
static readonly Lazy<ISecureBlobCache> _Current = new Lazy<ISecureBlobCache>(() => new CEncryptedBlobCache(GetDefaultCacheDirectory()));
static readonly Lazy<ISecureBlobCache> _Current = new Lazy<ISecureBlobCache>(() => {
var fs = default(IFilesystemProvider);
try {
fs = RxApp.GetService<IFilesystemProvider>();
} catch (Exception ex) {
LogHost.Default.DebugException("Couldn't find custom fs provider for secret cache", ex);
}
return new CEncryptedBlobCache(fs.GetDefaultSecretCacheDirectory(), fs);
});

public static ISecureBlobCache Current
{
get { return _Current.Value; }
Expand All @@ -28,11 +37,7 @@ public static ISecureBlobCache Current
}

class CEncryptedBlobCache : EncryptedBlobCache {
#if SILVERLIGHT
public CEncryptedBlobCache(string cacheDirectory) : base(cacheDirectory, new IsolatedStorageProvider(), RxApp.TaskpoolScheduler) { }
#else
public CEncryptedBlobCache(string cacheDirectory) : base(cacheDirectory, null, RxApp.TaskpoolScheduler) { }
#endif
public CEncryptedBlobCache(string cacheDirectory, IFilesystemProvider fsProvider) : base(cacheDirectory, fsProvider, RxApp.TaskpoolScheduler) { }
}

protected override IObservable<byte[]> BeforeWriteToDiskFilter(byte[] data, IScheduler scheduler)
Expand Down Expand Up @@ -70,15 +75,5 @@ protected override IObservable<byte[]> AfterReadFromDiskFilter(byte[] data, ISch
}
}

protected static string GetDefaultCacheDirectory()
{
#if SILVERLIGHT
return "SecretCache";
#else
return RxApp.InUnitTestRunner() ?
Path.Combine(GetAssemblyDirectoryName(), "SecretCache") :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), BlobCache.ApplicationName, "SecretCache");
#endif
}
}
}
18 changes: 18 additions & 0 deletions Akavache/IBlobCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ public interface IFilesystemProvider
/// </summary>
/// <param name="path">The path to the file</param>
IObservable<Unit> Delete(string path);

/// <summary>
/// Gets the default local machine cache directory (i.e. the one for temporary data)
/// </summary>
/// <returns>The default local machine cache directory.</returns>
string GetDefaultLocalMachineCacheDirectory();

/// <summary>
/// Gets the default roaming cache directory (i.e. the one for user settings)
/// </summary>
/// <returns>The default roaming cache directory.</returns>
string GetDefaultRoamingCacheDirectory();

/// <summary>
/// Gets the default roaming cache directory (i.e. the one for user settings)
/// </summary>
/// <returns>The default roaming cache directory.</returns>
string GetDefaultSecretCacheDirectory();
}

/// <summary>
Expand Down
15 changes: 15 additions & 0 deletions Akavache/IsolatedStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,20 @@ public IObservable<Unit> Delete(string path)
}
}, RxApp.TaskpoolScheduler);
}

public string GetDefaultRoamingCacheDirectory()
{
return "BlobCache";
}

public string GetDefaultSecretCacheDirectory()
{
return "SecretCache";
}

public string GetDefaultLocalMachineCacheDirectory()
{
return "LocalBlobCache";
}
}
}
88 changes: 33 additions & 55 deletions Akavache/PersistentBlobCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public abstract class PersistentBlobCache : IBlobCache, IEnableLogger
IScheduler scheduler = null,
Action<AsyncSubject<byte[]>> invalidateCallback = null)
{
this.CacheDirectory = cacheDirectory ?? GetDefaultRoamingCacheDirectory();
this.Scheduler = scheduler ?? RxApp.TaskpoolScheduler;
this.filesystem = filesystemProvider ?? new SimpleFilesystemProvider();
this.CacheDirectory = cacheDirectory ?? filesystem.GetDefaultRoamingCacheDirectory();
this.Scheduler = scheduler ?? RxApp.TaskpoolScheduler;

// Here, we're not actually caching the requests directly (i.e. as
// byte[]s), but as the "replayed result of the request", in the
Expand All @@ -63,7 +63,7 @@ public abstract class PersistentBlobCache : IBlobCache, IEnableLogger
{
var dir = filesystem.CreateRecursive(CacheDirectory);
#if WINRT
// NB: I don't want to talk about it.
// NB: I don't want to talk about it.
dir.Wait();
#endif
}
Expand Down Expand Up @@ -104,14 +104,41 @@ public IServiceProvider ServiceProvider
get { return BlobCache.ServiceProvider; }
}

static readonly Lazy<IBlobCache> _LocalMachine = new Lazy<IBlobCache>(() => new CPersistentBlobCache(GetDefaultLocalMachineCacheDirectory()));
static readonly Lazy<IBlobCache> _LocalMachine = new Lazy<IBlobCache>(() => {
var fs = default(IFilesystemProvider);
try {
fs = RxApp.GetService<IFilesystemProvider>();
} catch (Exception ex) {
LogHost.Default.DebugException("Couldn't find custom fs provider for local machine", ex);
}
#if SILVERLIGHT
fs = fs ?? new IsolatedStorageProvider();
#else
fs = fs ?? new SimpleFilesystemProvider();
#endif
return new CPersistentBlobCache(fs.GetDefaultLocalMachineCacheDirectory(), fs);
});

public static IBlobCache LocalMachine
{
get { return _LocalMachine.Value; }
}

static readonly Lazy<IBlobCache> _UserAccount = new Lazy<IBlobCache>(() => new CPersistentBlobCache(GetDefaultRoamingCacheDirectory()));

static readonly Lazy<IBlobCache> _UserAccount = new Lazy<IBlobCache>(() => {
var fs = default(IFilesystemProvider);
try {
fs = RxApp.GetService<IFilesystemProvider>();
} catch (Exception ex) {
LogHost.Default.DebugException("Couldn't find custom fs provider for user acct", ex);
}
#if SILVERLIGHT
fs = fs ?? new IsolatedStorageProvider();
#else
fs = fs ?? new SimpleFilesystemProvider();
#endif
return new CPersistentBlobCache(fs.GetDefaultRoamingCacheDirectory(), fs);
});

public static IBlobCache UserAccount
{
Expand All @@ -120,13 +147,7 @@ public static IBlobCache UserAccount

class CPersistentBlobCache : PersistentBlobCache
{
#if SILVERLIGHT
public CPersistentBlobCache(string cacheDirectory) : base(cacheDirectory, new IsolatedStorageProvider(), RxApp.TaskpoolScheduler) { }
#else
public CPersistentBlobCache(string cacheDirectory) : base(cacheDirectory, null, RxApp.TaskpoolScheduler)
{
}
#endif
public CPersistentBlobCache(string cacheDirectory, IFilesystemProvider fs) : base(cacheDirectory, fs, RxApp.TaskpoolScheduler) { }
}

public IObservable<Unit> Insert(string key, byte[] data, DateTimeOffset? absoluteExpiration = null)
Expand Down Expand Up @@ -427,49 +448,6 @@ string GetPathForKey(string key)
return Path.Combine(CacheDirectory, Utility.GetMd5Hash(key));
}

#if SILVERLIGHT
protected static string GetDefaultRoamingCacheDirectory()
{
return "BlobCache";
}

protected static string GetDefaultLocalMachineCacheDirectory()
{
return "LocalBlobCache";
}
#elif WINRT
protected static string GetDefaultRoamingCacheDirectory()
{
return Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, "BlobCache");
}

protected static string GetDefaultLocalMachineCacheDirectory()
{
return Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BlobCache");
}
#else
protected static string GetDefaultRoamingCacheDirectory()
{
return RxApp.InUnitTestRunner() ?
Path.Combine(GetAssemblyDirectoryName(), "BlobCache") :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), BlobCache.ApplicationName, "BlobCache");
}

protected static string GetDefaultLocalMachineCacheDirectory()
{
return RxApp.InUnitTestRunner() ?
Path.Combine(GetAssemblyDirectoryName(), "LocalBlobCache") :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), BlobCache.ApplicationName, "BlobCache");
}

protected static string GetAssemblyDirectoryName()
{
var assemblyDirectoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Debug.Assert(assemblyDirectoryName != null, "The directory name of the assembly location is null");
return assemblyDirectoryName;
}
#endif

static IObservable<byte[]> ObservableThrowKeyNotFoundException(string key, Exception innerException = null)
{
return Observable.Throw<byte[]>(
Expand Down
30 changes: 30 additions & 0 deletions Akavache/SimpleFilesystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using ReactiveUI;
using System.Reflection;
using System.Diagnostics;

namespace Akavache
{
Expand All @@ -24,5 +26,33 @@ public IObservable<Unit> Delete(string path)
{
return Observable.Start(() => File.Delete(path), RxApp.TaskpoolScheduler);
}

public string GetDefaultRoamingCacheDirectory()
{
return RxApp.InUnitTestRunner() ?
Path.Combine(GetAssemblyDirectoryName(), "BlobCache") :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), BlobCache.ApplicationName, "BlobCache");
}

public string GetDefaultSecretCacheDirectory()
{
return RxApp.InUnitTestRunner() ?
Path.Combine(GetAssemblyDirectoryName(), "SecretCache") :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), BlobCache.ApplicationName, "SecretCache");
}

public string GetDefaultLocalMachineCacheDirectory()
{
return RxApp.InUnitTestRunner() ?
Path.Combine(GetAssemblyDirectoryName(), "LocalBlobCache") :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), BlobCache.ApplicationName, "BlobCache");
}

protected static string GetAssemblyDirectoryName()
{
var assemblyDirectoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Debug.Assert(assemblyDirectoryName != null, "The directory name of the assembly location is null");
return assemblyDirectoryName;
}
}
}
17 changes: 16 additions & 1 deletion Akavache/WinRTFilesystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,20 @@ public IObservable<Unit> Delete(string path)
return StorageFile.GetFileFromPathAsync(path).ToObservable()
.SelectMany(x => x.DeleteAsync().ToObservable());
}

public string GetDefaultRoamingCacheDirectory()
{
return Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, "BlobCache");
}

public string GetDefaultLocalMachineCacheDirectory()
{
return Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BlobCache");
}

public string GetDefaultSecretCacheDirectory()
{
return Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, "SecretCache");
}
}
}
}

0 comments on commit 2efe6a3

Please sign in to comment.