-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRedisFunction.cs
More file actions
59 lines (53 loc) · 1.94 KB
/
RedisFunction.cs
File metadata and controls
59 lines (53 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Indigo.Functions.Redis;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using StackExchange.Redis;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace RedisFunctionSample
{
public static class RedisFunction
{
[FunctionName("Redis_GetKey")]
public static string GetKey(
[HttpTrigger("GET", Route = "cache/{key}")] HttpRequest req,
string key,
[Redis] IConnectionMultiplexer connectionMultiplexer)
{
var database = connectionMultiplexer.GetDatabase();
return database.StringGet(key);
}
[FunctionName("Redis_ListKeys")]
public static IEnumerable<string> ListKeys(
[HttpTrigger("GET", Route = "cache")] HttpRequest req,
[Redis] IConnectionMultiplexer connectionMultiplexer)
{
string pattern = req.Query["pattern"];
var randomEndpoint = connectionMultiplexer.GetEndPoints().First();
var server = connectionMultiplexer.GetServer(randomEndpoint);
var keys = server.Keys(pattern: pattern);
return keys.Select(x => x.ToString());
}
[FunctionName("Redis_SetKey")]
public static IActionResult SetKey(
[HttpTrigger("POST", Route = "cache/{key}")] HttpRequest req,
string key,
[Redis] IConnectionMultiplexer connectionMultiplexer)
{
string value = null;
using (var reader = new StreamReader(req.Body))
{
value = reader.ReadToEnd();
}
if (string.IsNullOrEmpty(value))
{
return new BadRequestObjectResult("No value specified");
}
var database = connectionMultiplexer.GetDatabase();
database.StringSet(key, value);
return new OkObjectResult($"{key} = {value}");
}
}
}