Repository navigation
Expand file tree
/
Copy pathRedisController.cs
More file actions
88 lines (75 loc) · 2.3 KB
/
Copy pathRedisController.cs
File metadata and controls
88 lines (75 loc) · 2.3 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis.Extensions.Core.Abstractions;
namespace RedisAspDotNetCoreWebApiSample.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RedisController: ControllerBase
{
private readonly IRedisCacheClient _redis;
public RedisController(IRedisCacheClient redis)
{
_redis = redis;
}
[HttpPost]
public IActionResult Post(User fakeuser)
{
var user = new User()
{
Firstname = "Taswar",
Lastname = "Bhatti",
Twitter = "@taswarbhatti",
Blog = "http://taswar.zeytinsoft.com"
};
bool added = _redis.Db0.Add("user:key", user, DateTimeOffset.Now.AddMinutes(10));
if(added)
return Ok();
else
return BadRequest("Cannot add user");
}
[HttpPost]
[Route("db/create")]
public IActionResult CreateSet()
{
//create using Redis Commands
bool added = _redis.Db0.Database.SetAdd("setkey","1");
if(added)
return Ok();
else
return BadRequest("Cannot add user");
}
[HttpGet]
[Route("db/getset")]
public ActionResult<string> GetSet()
{
var setValues = _redis.Db0.Database.SetMembers("setkey");
return string.Join(",", setValues);
}
[HttpGet]
public ActionResult<IEnumerable<User>> Get()
{
var users = _redis.Db0.GetAll<User>(new string[] {"user:key"});
return users.Values.ToList();
}
// DELETE api/redis/5
[HttpDelete("{key}")]
public ActionResult Delete(string key)
{
var result = _redis.Db0.Remove(key);
if(result)
return Ok();
else
return BadRequest("Cannot delete user");
}
}
public class User
{
public string Firstname { get; set;}
public string Lastname { get; set;}
public string Twitter { get; set;}
public string Blog { get; set;}
}
}