Skip to content

Commit

Permalink
Replace the cache service with local resource storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetjunky authored and Tim Lovell-Smith committed Dec 12, 2012
1 parent 926935e commit a7e07b5
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 79 deletions.
4 changes: 3 additions & 1 deletion NuGetGallery.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3D9D438F-93F6-4CDF-AABD-9990063BAD67}"
ProjectSection(SolutionItems) = preProject
README.markdown = README.markdown
Scripts\NuGetGallery.cscfg = Scripts\NuGetGallery.cscfg
Scripts\NuGetGallery.csdef = Scripts\NuGetGallery.csdef
Scripts\NuGetGallery.msbuild = Scripts\NuGetGallery.msbuild
README.markdown = README.markdown
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{96E4AFF8-D3A1-4102-ADCF-05F186F916A9}"
Expand Down
1 change: 1 addition & 0 deletions Scripts/NuGetGallery.csdef
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</Endpoints>
<LocalResources>
<LocalStorage name="IISLogs" sizeInMB="128" cleanOnRoleRecycle="false" />
<LocalStorage name="PackageCache" sizeInMB="200" cleanOnRoleRecycle="true" />
</LocalResources>
<Imports>
<Import moduleName="Diagnostics" />
Expand Down
30 changes: 0 additions & 30 deletions Scripts/Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
$azureDiagStorageAccessKey = $env:NUGET_GALLERY_AZURE_DIAG_STORAGE_ACCESS_KEY,
$azureDiagStorageAccountName = $env:NUGET_GALLERY_AZURE_DIAG_STORAGE_ACCOUNT_NAME,
$facebookAppId = $env:NUGET_FACEBOOK_APP_ID,
$cacheServiceEndpoint = $env:NUGET_GALLERY_CACHE_SERVICE_ENDPOINT,
$cacheServiceAccessKey = $env:NUGET_GALLERY_CACHE_SERVICE_ACCESS_KEY,
$commitSha,
$commitBranch
)
Expand Down Expand Up @@ -188,32 +186,6 @@ function set-machinekey {
}
}

function set-cacheserviceurl {
param($path, $value)

$settings = [xml](get-content $path)

$settings.configuration.dataCacheClients.dataCacheClient | % {
$_.hosts.host.name = $value
}

$resolvedPath = resolve-path($path)
$settings.save($resolvedPath)
}

function set-cacheserviceaccesskey {
param($path, $value)

$settings = [xml](get-content $path)

$settings.configuration.dataCacheClients.dataCacheClient | % {
$_.securityProperties.messageSecurity.authorizationInfo = $value
}

$resolvedPath = resolve-path($path)
$settings.save($resolvedPath)
}

function enable-azureElmah {
param($path)
$connectionString = "";
Expand Down Expand Up @@ -314,8 +286,6 @@ if(!$UseEmulator) {
set-appsetting -path $webConfigPath -name "Gallery:ReleaseTime" -value (Get-Date -format "dd/MM/yyyy HH:mm:ss")
set-appsetting -path $webConfigPath -name "Gallery:ReleaseTime" -value (Get-Date -format "dd/MM/yyyy HH:mm:ss")
set-appsetting -path $webConfigPath -name "Gallery:UseAzureEmulator" -value "false"
set-cacheserviceurl -path $webConfigPath -value $cacheServiceEndpoint
set-cacheserviceaccesskey -path $webConfigPath -value $cacheServiceAccessKey
}

if(![String]::IsNullOrEmpty($facebookAppId)) {
Expand Down
2 changes: 1 addition & 1 deletion Website/App_Start/ContainerBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override void Load()
{
// when running locally on dev box, use the built-in ASP.NET Http Cache
Bind<ICacheService>()
.To<HttpCacheService>()
.To<NullCacheService>()
.InSingletonScope();
}

Expand Down
2 changes: 1 addition & 1 deletion Website/Controllers/PackageFilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ private async Task<IPackageFile> GetPackageFile(string id, string version, strin
return packageFile;
}
}
}
}
4 changes: 2 additions & 2 deletions Website/Helpers/PackageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static async Task<IPackage> GetPackageFromCacheOrDownloadIt(
Debug.Assert(packageFileService != null);

string cacheKey = CreateCacheKey(package.PackageRegistration.Id, package.Version);
var buffer = cacheService.GetItem(cacheKey) as byte[];
byte[] buffer = cacheService.GetItem(cacheKey);
if (buffer == null)
{
using (Stream stream = await packageFileService.DownloadPackageFileAsync(package))
Expand All @@ -33,7 +33,7 @@ public static async Task<IPackage> GetPackageFromCacheOrDownloadIt(
}

buffer = stream.ReadAllBytes();
cacheService.SetItem(cacheKey, buffer, TimeSpan.FromMinutes(5));
cacheService.SetItem(cacheKey, buffer);
}
}

Expand Down
98 changes: 87 additions & 11 deletions Website/Services/CloudCacheService.cs
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)
{
}
}
}
}
}
}
30 changes: 0 additions & 30 deletions Website/Services/HttpCacheService.cs

This file was deleted.

4 changes: 2 additions & 2 deletions Website/Services/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NuGetGallery
{
public interface ICacheService
{
object GetItem(string key);
void SetItem(string key, object item, TimeSpan timeout);
byte[] GetItem(string key);
void SetItem(string key, byte[] data);
}
}
19 changes: 19 additions & 0 deletions Website/Services/NullCacheService.cs
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)
{
}
}
}
2 changes: 1 addition & 1 deletion Website/Website.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
<Compile Include="Queries\UserByUsernameQuery.cs" />
<Compile Include="RequestModels\ModifyCuratedPackageRequest.cs" />
<Compile Include="RequestModels\CreateCuratedPackageRequest.cs" />
<Compile Include="Services\HttpCacheService.cs" />
<Compile Include="Services\NullCacheService.cs" />
<Compile Include="Services\CloudBlobClientWrapper.cs" />
<Compile Include="Services\CloudBlobContainerWrapper.cs" />
<Compile Include="Services\CloudBlobFileStorageService.cs" />
Expand Down

0 comments on commit a7e07b5

Please sign in to comment.