Skip to content

Commit

Permalink
Remove cache function
Browse files Browse the repository at this point in the history
  • Loading branch information
adborroto committed Nov 4, 2020
1 parent da36a39 commit bfbc5a9
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 182 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Appconfi

[Appconfi](https://www.appconfi.com) - Service to centrally manage application settings and feature toggles for applications and services.
[Appconfi] - Service to centrally manage application settings and feature toggles for applications and services.

## Installation

Expand All @@ -12,7 +12,7 @@ More info is available on [nuget](https://www.nuget.org/packages/Appconfi/)

## Usage

In order to use the Appconfi you will need to [create an account](https://appconfi.com/account/register).
In order to use the Appconfi you will need to deploy an appconfi server and `/account/register`.

From there you can create your first application and setup your configuration. To use the Appconfi API to access your configuration go to `/accesskeys` there you can find the `application_id` and your `application_key`.

Expand Down Expand Up @@ -41,6 +41,3 @@ var refreshInterval = TimeSpan.FromMinutes(1);
var manager = Configuration.NewInstance(applicationId, apiKey, env, refreshInterval);
```

## Links

* [Web](https://appconfi.com)
8 changes: 4 additions & 4 deletions src/Appconfi.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class Program
{
static void Main(string[] args)
{
var applicationId = "530cfb60-da0a-491b-bad8-ff7122100bc1";
var apiKey = "655759a7573e480f9d727ddf5ae31264";
var env = "ES";
var applicationId = "<here>";
var apiKey = "<here>";
var env = "[default]";

var manager = Configuration.NewInstance(
new Uri("https://localhost:5001"),
new Uri("<here>"),
applicationId,
apiKey,
env,
Expand Down
16 changes: 6 additions & 10 deletions src/Appconfi.Test/AppconfiManagerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,35 +118,31 @@ public void AppconfiManager_IsFeatureEnabled_ValidKey_ReturnToggle()
}

[TestMethod]
public void AppconfiManager_IsFeatureEnabled_ValidKey_ReturnFromCache()
public void AppconfiManager_IsFeatureEnabled_ValidKey_ReturnFromDefault()
{
var configurationStoreMock = new Mock<IConfigurationStore>();
var configuration = new ApplicationConfiguration();

//Given a version
configurationStoreMock.Setup(x => x.GetVersion()).Returns("1");
//And given a cache
Func<string, bool> cache = (key) => key == "feature.toggle";

var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1), null, cache);
var isEnabled = manager.IsFeatureEnabled("feature.toggle");
var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1));
var isEnabled = manager.IsFeatureEnabled("feature.toggle", true);

Assert.IsTrue(isEnabled);
}

[TestMethod]
public void AppconfiManager_GetSetting_ValidKey_ReturnFromCache()
public void AppconfiManager_GetSetting_ValidKey_ReturnFromDefault()
{
var configurationStoreMock = new Mock<IConfigurationStore>();
var configuration = new ApplicationConfiguration();

//Given a version
configurationStoreMock.Setup(x => x.GetVersion()).Returns("1");
//And given a cache
Func<string, string> cache = (key) => key == "cache-setting" ? "value" : "invalid";

var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1), cache, null);
var value = manager.GetSetting("cache-setting");
var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1));
var value = manager.GetSetting("cache-setting", "value");

Assert.AreEqual("value", value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class WeatherForecastController : ControllerBase
};

private readonly ILogger<WeatherForecastController> _logger;
private readonly IFeatureManager _featureManager;
private readonly IAppconfiFeatureManager _featureManager;

public WeatherForecastController(ILogger<WeatherForecastController> logger, IFeatureManager featureManager)
public WeatherForecastController(ILogger<WeatherForecastController> logger, IAppconfiFeatureManager featureManager)
{
_logger = logger;
_featureManager = featureManager;
Expand All @@ -27,8 +27,10 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger, IFea
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
if (!_featureManager.IsEnabled("feature.a"))
return null;
if (!_featureManager.IsEnabled("awesome_feature"))
return Enumerable.Empty<WeatherForecast>() ;



var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
Expand Down
9 changes: 4 additions & 5 deletions src/Appconfi.Web.Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAppconfi(options => {
options.Application = "530cfb60-da0a-491b-bad8-ff7122100bc1";
options.Key = "655759a7573e480f9d727ddf5ae31264";
options.Environment = "ES";
options.BaseAddress = "https://localhost:5001";
options.Application = "<here>";
options.Key = "<here>";
options.Environment = "<here>";
options.BaseAddress = "<here>";
options.CacheExpirationTime = TimeSpan.FromMinutes(1);
options.UseFeatureToggleCache(typeof(Startup).Assembly, "features.json");
});
}

Expand Down
5 changes: 4 additions & 1 deletion src/Appconfi.Web/Appconfi.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<PackageId>Appconfi.Web</PackageId>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Authors>Appconfi's Team</Authors>
<Company>Appconfi</Company>
<Copyright>Copyright © Appconfi 2020</Copyright>
<RepositoryUrl>https://github.com/appconfi/appconfi-csharp</RepositoryUrl>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageReleaseNotes>
Version 1.3.0
* Remove cache function
* Simplify logic
Version 1.2.1
* Adding web extensions
</PackageReleaseNotes>
Expand Down
12 changes: 0 additions & 12 deletions src/Appconfi.Web/AppconfiOptions.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
using System;
using System.Reflection;

namespace Appconfi.Web
{
public class AppconfiOptions
{
public AppconfiCacheConfiguration FeatureToggleCacheConfiguration { get; private set; }
public string BaseAddress { get; set; }
public string Application { get; set; }
public string Key { get; set; }
public string Environment { get; set; } = "[default]";
public TimeSpan CacheExpirationTime { get; set; } = TimeSpan.FromMinutes(5);

public AppconfiOptions UseFeatureToggleCache(Assembly assembly, string resourceFileName)
{
FeatureToggleCacheConfiguration = new AppconfiCacheConfiguration
{
Assembly = assembly,
EmbeddedFilePath = resourceFileName
};
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Appconfi.Web.Extensions.DependencyInjection
{
public static class AppconfiServiceCollectionExtensions
{
private static Func<string, bool> GetLocalFeatures(AppconfiCacheConfiguration config)
{
if (config == null)
return null;

var embeddedResource = ResourceHelper.GetEmbeddedResource(config.EmbeddedFilePath, config.Assembly);
IDictionary<string,string> cache = JsonConvert.DeserializeObject<Dictionary<string,string>>(embeddedResource);
Func<string, bool> response = (feature) => {
if (cache.ContainsKey(feature))
return cache[feature] == "on";
return false;
};

return response;
}

public static IServiceCollection AddAppconfi(this IServiceCollection services, Action<AppconfiOptions> configure)
{
var options = new AppconfiOptions();
Expand All @@ -33,13 +15,11 @@ public static IServiceCollection AddAppconfi(this IServiceCollection services, A
options.Application,
options.Key,
options.Environment,
options.CacheExpirationTime,
null,
GetLocalFeatures(options.FeatureToggleCacheConfiguration)
options.CacheExpirationTime
);

manager.StartMonitor();
services.AddSingleton<IFeatureManager>(new FeatureManager(manager));
services.AddSingleton<IAppconfiFeatureManager>(new FeatureManager(manager));

return services;
}
Expand Down
11 changes: 8 additions & 3 deletions src/Appconfi.Web/FeatureManager.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
namespace Appconfi.Web
{
public class FeatureManager : IFeatureManager
public class FeatureManager : IAppconfiFeatureManager
{
private readonly AppconfiManager manager;

public FeatureManager(AppconfiManager manager)
{
this.manager = manager;
}
public bool IsEnabled(string featureName)
public bool IsEnabled(string featureName, bool defaultValue = false)
{
return manager.IsFeatureEnabled(featureName);
return manager.IsFeatureEnabled(featureName, defaultValue);
}

public void ForceRefresh()
{
manager.ForceRefresh();
}
}
}
8 changes: 8 additions & 0 deletions src/Appconfi.Web/IAppconfiFeatureManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Appconfi.Web
{
public interface IAppconfiFeatureManager
{
void ForceRefresh();
bool IsEnabled(string featureName, bool defaultValue = false);
}
}
7 changes: 0 additions & 7 deletions src/Appconfi.Web/IFeatureManager.cs

This file was deleted.

49 changes: 0 additions & 49 deletions src/Appconfi.Web/ResourceHelper.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Appconfi/AppconfiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public string PrepareRequest(string resource)
queryString["env"] = Environment;
queryString["app"] = ApplicationId;

return $"{resource}?{queryString.ToString()}";
return $"{resource}?{queryString}";
}

public string Execute(string resource)
Expand Down
Loading

0 comments on commit bfbc5a9

Please sign in to comment.