This commit is contained in:
hananiel 2019-09-27 11:10:43 -04:00
Родитель 152539b4b9
Коммит 6733a37cd9
17 изменённых файлов: 193 добавлений и 12 удалений

Просмотреть файл

@ -19,10 +19,13 @@ using System.Data;
using Npgsql;
using System.Data;
#endif
#if(MongoDB)
#if (MongoDB)
using MongoDB.Driver;
using System.Data;
#endif
#if (Redis)
using Microsoft.Extensions.Caching.Distributed;
#endif
namespace Company.WebApplication1.Controllers
{
#if (!NoAuth)
@ -122,6 +125,24 @@ namespace Company.WebApplication1.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
#endif
#if (Redis)
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
#endif
#if (!ValuesControllerWithArgs)
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -104,7 +104,7 @@ namespace Company.WebApplication1
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
#endif
#if (MongoDB)
services.AddMongoClient(Configuration);

Просмотреть файл

@ -23,6 +23,9 @@ using System.Data;
using MongoDB.Driver;
using System.Data;
#endif
#if(Redis)
using Microsoft.Extensions.Caching.Distributed;
#endif
namespace Company.WebApplication1.Controllers
{
#if (!NoAuth)
@ -121,6 +124,24 @@ namespace Company.WebApplication1.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
#endif
#if (Redis)
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
#endif
#if (!ValuesControllerWithArgs)
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -118,7 +118,7 @@ namespace Company.WebApplication1
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
#endif
#if (MongoDB)
services.AddMongoClient(Configuration);

Просмотреть файл

@ -23,6 +23,9 @@ using System.Data;
using MongoDB.Driver;
using System.Data;
#endif
#if(Redis)
using Microsoft.Extensions.Caching.Distributed;
#endif
namespace Company.WebApplication1.Controllers
{
#if (!NoAuth)
@ -122,6 +125,25 @@ namespace Company.WebApplication1.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
#endif
#if (Redis)
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
#endif
#if (!ValuesControllerWithArgs)
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -105,7 +105,7 @@ namespace Company.WebApplication1
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
#endif
#if (MongoDB)
services.AddMongoClient(Configuration);

Просмотреть файл

@ -19,10 +19,13 @@ using System.Data;
using Npgsql;
using System.Data;
#endif
#if(MongoDB)
#if (MongoDB)
using MongoDB.Driver;
using System.Data;
#endif
#if (Redis)
using Microsoft.Extensions.Caching.Distributed;
#endif
namespace Company.WebApplication1.Controllers
{
#if (!NoAuth)
@ -122,6 +125,24 @@ namespace Company.WebApplication1.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
#endif
#if (Redis)
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
#endif
#if (!ValuesControllerWithArgs)
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -122,7 +122,7 @@ namespace Company.WebApplication1
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
#endif
#if (MongoDB)
services.AddMongoClient(Configuration);

Просмотреть файл

@ -23,6 +23,9 @@ using System.Data;
using MongoDB.Driver;
using System.Data;
{{/MongoDB}}
{{#Redis}}
using Microsoft.Extensions.Caching.Distributed;
{{/Redis}}
namespace {{ProjectNameSpace}}.Controllers
{
{{#Auth}}
@ -120,6 +123,24 @@ namespace {{ProjectNameSpace}}.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
{{/MongoDB}}
{{#Redis}}
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
{{/Redis}}
{{^ValuesControllerWithArgs}}
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -110,11 +110,15 @@ namespace {{ProjectNameSpace}}
services.AddRabbitMQConnection(Configuration);
{{/RabbitMQ}}
{{#Redis}}
// In production, the React files will be served from this directory
// Add the Redis distributed cache.
// We are using the Steeltoe Redis Connector to pickup the CloudFoundry
// Redis Service binding and use it to configure the underlying RedisCache
// This adds a IDistributedCache to the container
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
{{/Redis}}
{{#MongoDB}}
services.AddMongoClient(Configuration);

Просмотреть файл

@ -23,6 +23,9 @@ using System.Data;
using MongoDB.Driver;
using System.Data;
{{/MongoDB}}
{{#Redis}}
using Microsoft.Extensions.Caching.Distributed;
{{/Redis}}
namespace {{ProjectNameSpace}}.Controllers
{
{{#Auth}}
@ -120,6 +123,24 @@ namespace {{ProjectNameSpace}}.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
{{/MongoDB}}
{{#Redis}}
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
{{/Redis}}
{{^ValuesControllerWithArgs}}
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -119,7 +119,7 @@ namespace {{ProjectNameSpace}}
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
{{/Redis}}
{{#MongoDB}}
services.AddMongoClient(Configuration);

Просмотреть файл

@ -23,6 +23,9 @@ using System.Data;
using MongoDB.Driver;
using System.Data;
{{/MongoDB}}
{{#Redis}}
using Microsoft.Extensions.Caching.Distributed;
{{/Redis}}
namespace {{ProjectNameSpace}}.Controllers
{
{{#Auth}}
@ -120,6 +123,25 @@ namespace {{ProjectNameSpace}}.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
{{/MongoDB}}
{{#Redis}}
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
{{/Redis}}
{{^ValuesControllerWithArgs}}
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -112,11 +112,10 @@ namespace {{ProjectNameSpace}}
services.AddRabbitMQConnection(Configuration);
{{/RabbitMQ}}
{{#Redis}}
// In production, the React files will be served from this directory
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
{{/Redis}}
{{#MongoDB}}
services.AddMongoClient(Configuration);

Просмотреть файл

@ -23,6 +23,9 @@ using System.Data;
using MongoDB.Driver;
using System.Data;
{{/MongoDB}}
{{#Redis}}
using Microsoft.Extensions.Caching.Distributed;
{{/Redis}}
namespace {{ProjectNameSpace}}.Controllers
{
{{#Auth}}
@ -120,6 +123,24 @@ namespace {{ProjectNameSpace}}.Controllers
return _mongoClient.ListDatabaseNames().ToList();
}
{{/MongoDB}}
{{#Redis}}
private readonly IDistributedCache _cache;
public ValuesController(IDistributedCache cache)
{
_cache = cache;
}
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
await _cache.SetStringAsync("MyValue1", "123");
await _cache.SetStringAsync("MyValue2", "456");
string myval1 = await _cache.GetStringAsync("MyValue1");
string myval2 = await _cache.GetStringAsync("MyValue2");
return new string[]{ myval1, myval2};
}
{{/Redis}}
{{^ValuesControllerWithArgs}}
[HttpGet]
public ActionResult<string> Get()

Просмотреть файл

@ -116,7 +116,7 @@ namespace {{ProjectNameSpace}}
services.AddDistributedRedisCache(Configuration);
// This works like the above, but adds a IConnectionMultiplexer to the container
services.AddRedisConnectionMultiplexer(Configuration);
// services.AddRedisConnectionMultiplexer(Configuration);
{{/Redis}}
{{#MongoDB}}
services.AddMongoClient(Configuration);

Просмотреть файл

@ -278,6 +278,14 @@ namespace Steeltoe.Initializr.Tests
string startUpContents = files.Find(x => x.Key == "Startup.cs").Value;
Assert.Contains("using Steeltoe.CloudFoundry.Connector.Redis", startUpContents);
Assert.Contains("services.AddDistributedRedisCache(Configuration);", startUpContents);
Assert.Contains("// services.AddRedisConnectionMultiplexer(Configuration);", startUpContents);
string valuesController = files.Find(x => x.Key == "Controllers\\ValuesController.cs").Value;
Assert.Contains("using Microsoft.Extensions.Caching.Distributed;", valuesController);
Assert.Contains(@" public ValuesController(IDistributedCache cache)", valuesController);
Assert.Contains(@"await _cache.SetStringAsync(""MyValue1"", ""123"");", valuesController);
}
[Theory]