Created
June 29, 2020 22:39
-
-
Save dominiqueplante/04f879701ec51184c6fbc24a53f9f8bb to your computer and use it in GitHub Desktop.
connect to redis from .NET 3.1 console app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApp1 | |
{ | |
using System; | |
using ServiceStack.Redis; | |
using ServiceStack.Text; | |
public class Todo | |
{ | |
public long Id { get; set; } | |
public string Content { get; set; } | |
public int Order { get; set; } | |
public bool Done { get; set; } | |
} | |
# https://jeremylindsayni.wordpress.com/2016/11/24/connect-to-a-redis-container-hosted-in-docker-from-a-net-core-application/ | |
# start redis @ docker run -d --name myRedis -p 6379:6379 redis | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
var redisManager = new RedisManagerPool("localhost:6379"); | |
var redis = redisManager.GetClient(); | |
var redisTodos = redis.As<Todo>(); | |
var newTodo = new Todo | |
{ | |
Id = redisTodos.GetNextSequence(), | |
Content = "Learn Redis", | |
Order = 1, | |
}; | |
redisTodos.Store(newTodo); | |
Todo savedTodo = redisTodos.GetById(newTodo.Id); | |
"Saved Todo: {0}".Print(savedTodo.Dump()); | |
savedTodo.Done = true; | |
redisTodos.Store(savedTodo); | |
var updatedTodo = redisTodos.GetById(newTodo.Id); | |
"Updated Todo: {0}".Print(updatedTodo.Dump()); | |
redisTodos.DeleteById(newTodo.Id); | |
var remainingTodos = redisTodos.GetAll(); | |
"No more Todos:".Print(remainingTodos.Dump()); | |
} | |
} | |
} | |
/* | |
using System; | |
using ServiceStack; | |
using ServiceStack.Text; | |
using ServiceStack.Redis; | |
using ServiceStack.DataAnnotations; | |
var redisManager = new RedisManagerPool("localhost:6379"); | |
var redis = redisManager.GetClient(); | |
var redisTodos = redis.As<Todo>(); | |
var newTodo = new Todo | |
{ | |
Id = redisTodos.GetNextSequence(), | |
Content = "Learn Redis", | |
Order = 1, | |
}; | |
redisTodos.Store(newTodo); | |
Todo savedTodo = redisTodos.GetById(newTodo.Id); | |
"Saved Todo: {0}".Print(savedTodo.Dump()); | |
savedTodo.Done = true; | |
redisTodos.Store(savedTodo); | |
var updatedTodo = redisTodos.GetById(newTodo.Id); | |
"Updated Todo: {0}".Print(updatedTodo.Dump()); | |
redisTodos.DeleteById(newTodo.Id); | |
var remainingTodos = redisTodos.GetAll(); | |
"No more Todos:".Print(remainingTodos.Dump()); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment