Created
June 21, 2015 20:27
-
-
Save JeffreyMFarley/93a454ba9a0998ffc912 to your computer and use it in GitHub Desktop.
Internet Caching Repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.Composition; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using Eastwind.Entities; | |
using Esoteric; | |
using Esoteric.DAL.Interfaces; | |
namespace Eastwind.DAL.Repositories | |
{ | |
[Export] | |
[Export(typeof(IRandomAccessRepository<byte[], IQuery>))] | |
public class InternetQueryCompositeRepository : RandomAccessRepository<byte[], IQuery> | |
{ | |
#region Properties | |
//----------------------------------------------------------------------------------- | |
[Import] | |
public IRandomAccessRepository<DirectoryInfo, string> PathConfig | |
{ | |
get; | |
set; | |
} | |
[Import] | |
public InternetQueryDiskRepository DiskCache | |
{ | |
get; | |
set; | |
} | |
[Import] | |
public MemoryRepositoryByteArray MemoryCache | |
{ | |
get; | |
set; | |
} | |
[Import] | |
public InternetRepositoryByteArray TehIntertubes | |
{ | |
get; | |
set; | |
} | |
#endregion | |
#region RandomAccessRepository<byte[], IQuery> members | |
//----------------------------------------------------------------------------- | |
public override IEnumerator<byte[]> GetEnumerator() | |
{ | |
var v = from s in MemoryCache.Union(DiskCache) | |
where s != null | |
select s; | |
return v.GetEnumerator(); | |
} | |
public override byte[] Get(IQuery query) | |
{ | |
Debug.Assert(query != null); | |
Uri uri = query.RequestUri; | |
Debug.Assert(uri != null); | |
// see if it is in the cache | |
byte[] result = MemoryCache.Get(uri); | |
if (result != null) | |
return result; | |
// see if it is on disk | |
result = DiskCache.Get(CreateFileName(query)); | |
if (result != null) | |
return result; | |
// Get it from the internet | |
result = TehIntertubes.Get(uri); | |
// Save it | |
if( result != null ) | |
Set(result, query); | |
return result; | |
} | |
public override byte[] Set(byte[] item, IQuery query) | |
{ | |
Debug.Assert(query != null); | |
// Save it to the memory cache | |
MemoryCache.Set(item, query.RequestUri); | |
// Save to disk | |
FileInfo fi = CreateFileName(query); | |
if( fi != null ) | |
DiskCache.Set(item, fi); | |
return item; | |
} | |
public override bool Delete(IQuery query) | |
{ | |
MemoryCache.Delete(query.RequestUri); | |
FileInfo fi = CreateFileName(query); | |
DiskCache.Delete(fi); | |
return true; | |
} | |
public override IQuery GetKey(byte[] item) | |
{ | |
throw new NotSupportedException(); | |
} | |
#endregion | |
FileInfo CreateFileName(IQuery query) | |
{ | |
Debug.Assert(query != null); | |
Uri uri = query.RequestUri; | |
string luid = query.Luid; | |
// bouncer code | |
if (String.IsNullOrEmpty(luid)) | |
return null; | |
// Get the service name | |
string[] segments = uri.Host.Split('.'); | |
if (segments.Length < 2) return null; | |
string serviceName = Path.DirectorySeparatorChar + segments[segments.Length - 2]; | |
// see where a cached version would be | |
string strPath = PathConfig.Get("WebServiceResultsRoot").FullName + serviceName; | |
if (!Directory.Exists(strPath)) | |
Directory.CreateDirectory(strPath); | |
return new FileInfo(strPath + Path.DirectorySeparatorChar + luid.RemoveBadFileNameChars() + ".xml"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment