-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace the cache service with local resource storage.
- Loading branch information
dotnetjunky
authored and
Tim Lovell-Smith
committed
Dec 12, 2012
1 parent
926935e
commit a7e07b5
Showing
11 changed files
with
117 additions
and
79 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,4 +118,4 @@ private async Task<IPackageFile> GetPackageFile(string id, string version, strin | |
return packageFile; | ||
} | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,100 @@ | ||
using System; | ||
using Microsoft.ApplicationServer.Caching; | ||
using System.IO; | ||
using System.Security; | ||
using Microsoft.WindowsAzure.ServiceRuntime; | ||
|
||
namespace NuGetGallery | ||
{ | ||
public class CloudCacheService : ICacheService | ||
{ | ||
// The DataCacheFactory object is expensive. | ||
// It should be created only once per app domain. | ||
private static readonly DataCacheFactory _cacheFactory = new DataCacheFactory(); | ||
private readonly string _rootPath; | ||
|
||
public object GetItem(string key) | ||
public CloudCacheService() | ||
{ | ||
DataCache dataCache = _cacheFactory.GetDefaultCache(); | ||
return dataCache.Get(key); | ||
try | ||
{ | ||
LocalResource localResource = RoleEnvironment.GetLocalResource("PackageCache"); | ||
_rootPath = localResource.RootPath; | ||
} | ||
catch (RoleEnvironmentException exception) | ||
{ | ||
throw new InvalidOperationException("The local resource isn't defined.", exception); | ||
} | ||
} | ||
|
||
public void SetItem(string key, object item, TimeSpan timeout) | ||
public byte[] GetItem(string key) | ||
{ | ||
DataCache dataCache = _cacheFactory.GetDefaultCache(); | ||
dataCache.Put(key, item, timeout); | ||
if (String.IsNullOrEmpty(key)) | ||
{ | ||
throw new ArgumentException("'key' cannot be null or empty.", "key"); | ||
} | ||
|
||
string filePath = Path.Combine(_rootPath, key); | ||
if (!File.Exists(filePath)) | ||
{ | ||
return null; | ||
} | ||
|
||
return File.ReadAllBytes(filePath); | ||
} | ||
|
||
public void SetItem(string key, byte[] item) | ||
{ | ||
if (String.IsNullOrEmpty(key)) | ||
{ | ||
throw new ArgumentException("'key' cannot be null or empty.", "key"); | ||
} | ||
|
||
string filePath = Path.Combine(_rootPath, key); | ||
|
||
try | ||
{ | ||
if (File.Exists(filePath)) | ||
{ | ||
File.Delete(filePath); | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
File.WriteAllBytes(filePath, item); | ||
} | ||
catch (Exception) | ||
{ | ||
// One of the possible reasons for this exception is that we exceed the quota of local resource. | ||
// In that case, delete all files and try again. | ||
DeleteAllFiles(); | ||
|
||
try | ||
{ | ||
File.WriteAllBytes(filePath, item); | ||
} | ||
catch (Exception) | ||
{ | ||
// if the second attempt still fails, move on | ||
} | ||
} | ||
} | ||
|
||
private void DeleteAllFiles() | ||
{ | ||
foreach (var file in Directory.EnumerateFiles(_rootPath)) | ||
{ | ||
try | ||
{ | ||
File.Delete(file); | ||
} | ||
catch (IOException) | ||
{ | ||
} | ||
catch (SecurityException) | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace NuGetGallery | ||
{ | ||
/// <summary> | ||
/// A cache service that does not store objects at all. | ||
/// </summary> | ||
public sealed class NullCacheService : ICacheService | ||
{ | ||
public byte[] GetItem(string key) | ||
{ | ||
return null; | ||
} | ||
|
||
public void SetItem(string key, byte[] item) | ||
{ | ||
} | ||
} | ||
} |
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