Permalink
Browse files

removed use of distributed cache; created new, tight queries for pack…

…age IDs and versions
  • Loading branch information...
1 parent fffe9d3 commit f17ca2af92f6afb2635f4573c776184813b9f2dd @half-ogre half-ogre committed Jun 5, 2012
@@ -426,33 +426,6 @@ void WillThrowIfTheNuGetPackageTitleIsLongerThan4000()
Assert.Equal(String.Format(Strings.NuGetPackagePropertyTooLong, "Title", "4000"), ex.Message);
}
-
- [Fact]
- void WillRemoveTheCachedPackagesTabCompletionInfo()
- {
- var currentUser = new User();
- var packageRegistration = new PackageRegistration
- {
- Id = "theId",
- Owners = new HashSet<User> { currentUser },
- };
- var packageRegistrationRepo = new Mock<IEntityRepository<PackageRegistration>>();
- var stubCache = new Mock<ICache>();
- var service = CreateService(
- packageRegistrationRepo: packageRegistrationRepo,
- stubCache: stubCache,
- setup: mockPackageSvc =>
- {
- mockPackageSvc.Setup(x => x.FindPackageRegistrationById(It.IsAny<string>())).Returns(packageRegistration);
- });
- var nugetPackage = CreateNuGetPackage();
-
- service.CreatePackage(
- nugetPackage.Object,
- currentUser);
-
- stubCache.Verify(stub => stub.Remove(Constants.PackageIdsCacheKey));
- }
}
public class TheDeletePackageMethod
@@ -1273,8 +1246,7 @@ static Mock<IPackage> CreateNuGetPackage(Action<Mock<IPackage>> setup = null)
Mock<IPackageFileService> packageFileSvc = null,
Mock<IEntityRepository<PackageOwnerRequest>> packageOwnerRequestRepo = null,
Mock<IIndexingService> indexingSvc = null,
- Action<Mock<PackageService>> setup = null,
- Mock<ICache> stubCache = null)
+ Action<Mock<PackageService>> setup = null)
{
if (cryptoSvc == null)
{
@@ -1289,7 +1261,6 @@ static Mock<IPackage> CreateNuGetPackage(Action<Mock<IPackage>> setup = null)
packageStatsRepo = packageStatsRepo ?? new Mock<IEntityRepository<PackageStatistics>>();
packageOwnerRequestRepo = packageOwnerRequestRepo ?? new Mock<IEntityRepository<PackageOwnerRequest>>();
indexingSvc = indexingSvc ?? new Mock<IIndexingService>();
- stubCache = stubCache ?? new Mock<ICache>();
var packageSvc = new Mock<PackageService>(
cryptoSvc.Object,
@@ -1298,8 +1269,7 @@ static Mock<IPackage> CreateNuGetPackage(Action<Mock<IPackage>> setup = null)
packageStatsRepo.Object,
packageFileSvc.Object,
packageOwnerRequestRepo.Object,
- indexingSvc.Object,
- stubCache.Object);
+ indexingSvc.Object);
packageSvc.CallBase = true;
@@ -71,16 +71,6 @@ static bool RunningInAzure()
return false;
}
- public string AzureCacheAuthToken
- {
- get { return ReadConfiguration("AzureCacheAuthToken"); }
- }
-
- public string AzureCacheEndpoint
- {
- get { return ReadConfiguration("AzureCacheEndpoint"); }
- }
-
public string AzureStorageAccessKey
{
get
@@ -2,18 +2,15 @@
using System.Linq;
using System.Net;
using System.Net.Mail;
-using System.Security;
using System.Security.Principal;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using AnglicanGeek.MarkdownMailer;
-using Microsoft.ApplicationServer.Caching;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Ninject;
using Ninject.Modules;
-using NuGetGallery.Infrastructure;
namespace NuGetGallery
{
@@ -139,34 +136,16 @@ public override void Load()
Bind<IPrincipal>().ToMethod(context => HttpContext.Current.User);
- switch (configuration.PackageStoreType)
+ switch (configuration.PackageStoreType)
{
case PackageStoreType.FileSystem:
case PackageStoreType.NotSpecified:
- Bind<ICache>()
- .To<LocalCache>()
- .InSingletonScope();
Bind<IFileStorageService>()
.To<FileSystemFileStorageService>()
.InSingletonScope();
break;
case PackageStoreType.AzureStorageBlob:
- Bind<ICache>()
- .ToMethod(context =>
- {
- var dataCacheServerEndpoints = new DataCacheServerEndpoint[1];
- dataCacheServerEndpoints[0] = new DataCacheServerEndpoint(configuration.AzureCacheEndpoint, 22233);
- var authToken = configuration.AzureCacheAuthToken;
- var secureAuthToken = new SecureString();
- foreach (var a in authToken)
- secureAuthToken.AppendChar(a);
- secureAuthToken.MakeReadOnly();
- var dataCacheSecurity = new DataCacheSecurity(secureAuthToken);
- var dataCacheFactoryConfiguration = new DataCacheFactoryConfiguration { Servers = dataCacheServerEndpoints, SecurityProperties = dataCacheSecurity };
- var dataCacheFactory = new DataCacheFactory(dataCacheFactoryConfiguration);
- return new AzureCache(dataCacheFactory.GetDefaultCache());
- })
- .InSingletonScope();
+
Bind<ICloudBlobClient>()
.ToMethod(context => new CloudBlobClientWrapper(new CloudBlobClient(
new Uri(configuration.AzureStorageBlobUrl, UriKind.Absolute),
@@ -229,9 +208,12 @@ public override void Load()
Bind<IUserByUsernameQuery>()
.To<UserByUsernameQuery>()
.InRequestScope();
- Bind<IAllPackageRegistrationsQuery>()
- .To<AllPackageRegistrationsQuery>()
- .InRequestScope();
+ Bind<IPackageIdsQuery>()
+ .To<PackageIdsQuery>()
+ .InRequestScope();
+ Bind<IPackageVersionsQuery>()
+ .To<PackageVersionsQuery>()
+ .InRequestScope();
}
}
}
@@ -3,8 +3,6 @@ namespace NuGetGallery
{
public interface IConfiguration
{
- string AzureCacheAuthToken { get; }
- string AzureCacheEndpoint { get; }
string AzureStorageAccessKey { get; }
string AzureStorageAccountName { get; }
string AzureStorageBlobUrl { get; }
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
@@ -197,88 +198,17 @@ protected internal virtual IPackage ReadPackageFromRequest()
string partialId,
bool? includePrerelease)
{
- var cache = GetService<ICache>();
- var packageIds = cache.Get(Constants.PackageIdsCacheKey) as List<PackageId>;
- if (packageIds == null)
- {
- packageIds = GetService<IAllPackageRegistrationsQuery>()
- .Execute()
- .Where(pr => pr.Packages.Any(p => p.Listed))
- .OrderBy(pr => pr.DownloadCount)
- .Select(pr => new PackageId
- {
- Id = pr.Id,
- IsPrereleaseOnly = !pr.Packages.Any(p => p.IsLatestStable)
- })
- .ToList();
- cache.Add(Constants.PackageIdsCacheKey, packageIds);
- }
- var query = packageIds.AsQueryable();
- if (!includePrerelease.HasValue || !includePrerelease.Value)
- query = query.Where(x => !x.IsPrereleaseOnly);
- if (!string.IsNullOrWhiteSpace(partialId))
- query = query.Where(x => x.Id.StartsWith(partialId, StringComparison.OrdinalIgnoreCase));
- var result = query
- .Take(30)
- .Select(x => x.Id)
- .ToArray();
- return new JsonNetResult(result);
+ var qry = GetService<IPackageIdsQuery>();
+ return new JsonNetResult(qry.Execute(partialId, includePrerelease).ToArray());
}
[ActionName("PackageVersions"), HttpGet]
public virtual ActionResult GetPackageVersions(
string id,
bool? includePrerelease)
{
- if (string.IsNullOrWhiteSpace(id))
- return new JsonNetResult(new string[] {});
- var cache = GetService<ICache>();
- var cacheKey = string.Format(Constants.PackageVersionsCacheKeyFormat, id);
- var packageVersions = cache.Get(cacheKey) as PackageVersions;
- if (packageVersions == null)
- {
- var packageRegistration = GetService<IPackageRegistrationByIdQuery>().Execute(
- id,
- includePackages: true,
- includeOwners: false);
- packageVersions = new PackageVersions
- {
- Id = packageRegistration.Id,
- Versions = packageRegistration.Packages
- .Select(p => new PackageVersion
- {
- Version = p.Version,
- IsPrerelease = p.IsPrerelease
- })
- .ToList()
- };
- cache.Add(cacheKey, packageVersions);
- }
- var query = packageVersions.Versions.AsQueryable();
- if (!includePrerelease.HasValue || !includePrerelease.Value)
- query = query.Where(x => !x.IsPrerelease);
- var result = query
- .Select(x => x.Version)
- .ToArray();
- return new JsonNetResult(result);
- }
-
- class PackageId
- {
- public string Id { get; set; }
- public bool IsPrereleaseOnly { get; set; }
- }
-
- class PackageVersions
- {
- public string Id { get; set; }
- public List<PackageVersion> Versions { get; set; }
- }
-
- class PackageVersion
- {
- public bool IsPrerelease { get; set; }
- public string Version { get; set; }
+ var qry = GetService<IPackageVersionsQuery>();
+ return new JsonNetResult(qry.Execute(id, includePrerelease).ToArray());
}
}
}
@@ -1,33 +0,0 @@
-using System;
-using Microsoft.ApplicationServer.Caching;
-
-namespace NuGetGallery.Infrastructure
-{
- public class AzureCache : ICache
- {
- private readonly DataCache _dataCache;
-
- public AzureCache(DataCache dataCache)
- {
- if (dataCache == null)
- throw new ArgumentNullException("dataCache");
-
- _dataCache = dataCache;
- }
-
- public void Add(string key, object value)
- {
- _dataCache.Add(key, value);
- }
-
- public object Get(string key)
- {
- return _dataCache.Get(key);
- }
-
- public void Remove(string key)
- {
- _dataCache.Remove(key);
- }
- }
-}
@@ -1,9 +0,0 @@
-namespace NuGetGallery
-{
- public interface ICache
- {
- void Add(string key, object value);
- object Get(string key);
- void Remove(string key);
- }
-}
@@ -1,25 +0,0 @@
-using System.Collections.Generic;
-
-namespace NuGetGallery.Infrastructure
-{
- public class LocalCache : ICache
- {
- private readonly Dictionary<string, object> _cache = new Dictionary<string, object>();
-
- public void Add(string key, object value)
- {
- _cache[key] = value;
- }
-
- public object Get(string key)
- {
- object value;
- return _cache.TryGetValue(key, out value) ? value : null;
- }
-
- public void Remove(string key)
- {
- _cache.Remove(key);
- }
- }
-}
@@ -1,26 +0,0 @@
-using System.Collections.Generic;
-using System.Data.Entity;
-using System.Linq;
-
-namespace NuGetGallery
-{
- public interface IAllPackageRegistrationsQuery
- {
- IEnumerable<PackageRegistration> Execute();
- }
-
- public class AllPackageRegistrationsQuery : IAllPackageRegistrationsQuery
- {
- private readonly IEntitiesContext _entities;
-
- public AllPackageRegistrationsQuery(IEntitiesContext entities)
- {
- _entities = entities;
- }
-
- public IEnumerable<PackageRegistration> Execute()
- {
- return _entities.PackageRegistrations.AsQueryable().Include(x => x.Packages);
- }
- }
-}
Oops, something went wrong.

0 comments on commit f17ca2a

Please sign in to comment.