-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathProgram.cs
40 lines (34 loc) · 1.37 KB
/
Program.cs
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
using System;
using System.IO;
using System.Text;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.StackExchangeRedis;
namespace redis
{
public class RedisHandler
{
private static String host = Environment.GetEnvironmentVariable("REDIS_HOST");
private static String port = Environment.GetEnvironmentVariable("REDIS_PORT");
private static String passwd = Environment.GetEnvironmentVariable("REDIS_PASSWORD");
private static String connStr = $"{host}:{port},password={passwd},connectTimeout=5000,writeBuffer=40960";
public Stream Handler(Stream input, IFcContext context)
{
RedisCache cache = new RedisCache(new RedisCacheOptions
{
Configuration = connStr
});
var counterBytes = cache.Get("dotnet_counter");
var counter = 0;
if (counterBytes != null)
{
counter = BitConverter.ToInt32(counterBytes, 0);
}
Console.WriteLine($"Counter: {counter}");
cache.Set("dotnet_counter", BitConverter.GetBytes(counter + 1));
counterBytes = BitConverter.GetBytes(counter);
MemoryStream output = new MemoryStream(counterBytes);
return output;
}
}
}