Skip to content

Instantly share code, notes, and snippets.

@igayamaguchi
Last active March 22, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igayamaguchi/af647f005a801ac3dba6433e3d8820f0 to your computer and use it in GitHub Desktop.
Save igayamaguchi/af647f005a801ac3dba6433e3d8820f0 to your computer and use it in GitHub Desktop.
C# Redis Samples
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace RedisSamples
{
class Program
{
static void Main(string[] args)
{
string key;
IDatabase cache;
try
{
cache = Connection.GetDatabase();
}
catch (RedisConnectionException e)
{
WriteException("接続失敗", e);
return;
}
cache.StringSet("key1", "value");
cache.StringSet("key2", 25);
key = "not-exist";
try
{
var x = cache.StringGet(key);
var notExist = JsonConvert.DeserializeObject<EmployeeDto>(cache.StringGet(key));
}
catch (ArgumentException e)
{
WriteException("キーが存在しない場合", e);
}
key = "other-type";
HashEntry[] hashEntries = {
new HashEntry("a", 1),
};
cache.HashSet(key, hashEntries);
try
{
var hash = JsonConvert.DeserializeObject<EmployeeDto>(cache.StringGet(key));
}
catch (RedisServerException e)
{
WriteException("String型じゃない場合", e);
}
key = "e25";
var employeeDto = new EmployeeDto() { Employee = new Employee(25, "Clayton Gragg") };
cache.StringSet(key, JsonConvert.SerializeObject(employeeDto));
var e25 = JsonConvert.DeserializeObject<EmployeeDto>(cache.StringGet(key));
Write(key, e25);
key = "anonymous";
var objDto = new { T = "a" };
cache.StringSet(key, JsonConvert.SerializeObject(objDto));
var eAnonymous = JsonConvert.DeserializeObject<EmployeeDto>(cache.StringGet(key));
try
{
Write(key, eAnonymous);
}
catch (NullReferenceException e)
{
WriteException("パースするJSONとマッピング対象のクラスが一致しない場合", e);
}
key = "string";
var stringDto = "test";
cache.StringSet(key, stringDto);
EmployeeDto str = null;
try
{
str = JsonConvert.DeserializeObject<EmployeeDto>(cache.StringGet(key));
Write(key, str);
}
catch (JsonReaderException e)
{
WriteException("中身がJSONではなくただの文字列の場合", e);
}
key = "array";
var badJsonDto = "[1, 2, 3]";
cache.StringSet(key, badJsonDto);
try
{
var arr = JsonConvert.DeserializeObject<EmployeeDto>(cache.StringGet(key));
Write(key, arr);
}
catch (JsonSerializationException e)
{
WriteException("中身が配列の場合", e);
}
}
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
return ConnectionMultiplexer.Connect("localhost");
});
public static ConnectionMultiplexer Connection
{
get
{
// RedisConnectionException 接続できなかったときや接続が切れたとき
return lazyConnection.Value;
}
}
private static void Write(string key, EmployeeDto dto)
{
var employee = dto.Employee;
Console.WriteLine($"{key}: {employee.Id} = {employee.Name}");
}
private static void WriteException(string when, _Exception e)
{
Console.WriteLine($"{when}: {e.GetType().Name} -> {e.Message}");
}
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public Employee(int EmployeeId, string Name)
{
this.Id = EmployeeId;
this.Name = Name;
}
}
public class EmployeeDto
{
public Employee Employee { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment