Browse Source

add redis save for IEGSaveObject

master
jkpete 4 months ago
parent
commit
c28ce70106
  1. 5
      addons/EGFramework/Module/SaveTools/EGDapper.cs
  2. 11
      addons/EGFramework/Module/SaveTools/EGMysqlSave.cs
  3. 88
      addons/EGFramework/Module/SaveTools/EGRedisSave.cs
  4. 14
      addons/EGFramework/Module/SaveTools/EGSqliteSave.cs

5
addons/EGFramework/Module/SaveTools/EGDapper.cs

@ -5,14 +5,11 @@ using System.Linq; @@ -5,14 +5,11 @@ using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Dapper;
//ORM Save tools. First support SQLite and MySQL,In future we will support other Database who implement DBConnection.
namespace EGFramework
{
public abstract class EGDapper : IEGSave, IEGSaveData, IEGCanGetDBConnection
{
public string DBName = "Default";
private string DefaultDBFolder = "SaveData";
public DbConnection Connection { get; set; }
public string ExceptionMsg;

11
addons/EGFramework/Module/SaveTools/EGMysqlSave.cs

@ -1,20 +1,12 @@ @@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using MySql.Data.MySqlClient;
using Dapper;
using System.Reflection;
using System.Linq;
using System.Data.Common;
//ORM Save tools. First support SQLite and MySQL,In future we will support other Database who implement DBConnection.
namespace EGFramework{
/// <summary>
/// This Class used Dapper for operate MySQL database.
/// </summary>
public class EGMysqlSave : EGDapper
{
private string Conn { set; get; }
public string Conn { set; get; }
public bool IsInit { set; get; }
/// <summary>
/// "server="+Address+";port="+Port+";uid="+UserName+";pwd="+Password+";database="+DataBase+";"
@ -26,6 +18,7 @@ namespace EGFramework{ @@ -26,6 +18,7 @@ namespace EGFramework{
{
Connection = new MySqlConnection(conn);
IsInit = true;
this.Conn = conn;
}
catch (System.Exception e)
{

88
addons/EGFramework/Module/SaveTools/EGRedisSave.cs

@ -0,0 +1,88 @@ @@ -0,0 +1,88 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using StackExchange.Redis;
namespace EGFramework{
public class EGRedisSave : IEGSave, IEGSaveObject
{
public string Conn { set; get; }
public bool IsInit { set; get; }
public ConnectionMultiplexer Redis { set; get; }
public IDatabase Database { set; get; }
public ISubscriber Subscriber { set; get; }
/// <summary>
/// connect to redis server
/// </summary>
/// <param name="conn"> such as server1:6379,server2:6379 </param>
public void InitSave(string conn)
{
try
{
Redis = ConnectionMultiplexer.Connect(conn);
IsInit = true;
this.Conn = conn;
Redis = ConnectionMultiplexer.Connect(conn);
Database = Redis.GetDatabase();
Subscriber = Redis.GetSubscriber();
}
catch (System.Exception)
{
//EG.Print("e:" + e);
}
}
public void AddObject<TObject>(string objectKey, TObject obj)
{
if(!Database.KeyExists(objectKey)){
Database.SetAdd(objectKey, JsonConvert.SerializeObject(obj));
}else{
throw new System.Exception("Key already exists in redis database.");
}
}
public void UpdateObject<TObject>(string objectKey, TObject obj)
{
if(Database.KeyExists(objectKey)){
Database.SetAdd(objectKey, JsonConvert.SerializeObject(obj));
}else{
throw new System.Exception("Key not exists in redis database.");
}
}
public void RemoveObject<TObject>(string objectKey)
{
Database.KeyDelete(objectKey);
}
public void SetObject<TObject>(string objectKey, TObject obj)
{
Database.SetAdd(objectKey, JsonConvert.SerializeObject(obj));
}
public TObject GetObject<TObject>(string objectKey) where TObject : new()
{
try
{
string result = Database.StringGet(objectKey);
TObject resultObj = JsonConvert.DeserializeObject<TObject>(result);
return resultObj;
}
catch (System.Exception)
{
throw;
}
}
public bool ContainsKey(string objectKey)
{
return Database.KeyExists(objectKey);
}
public IEnumerable<string> GetKeys()
{
throw new System.NotImplementedException();
}
}
}

14
addons/EGFramework/Module/SaveTools/EGSqliteSave.cs

@ -1,23 +1,17 @@ @@ -1,23 +1,17 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Dapper;
using Microsoft.Data.Sqlite;
namespace EGFramework{
public class EGSqliteSave : EGDapper
{
private string Conn { set; get; }
public string Conn { set; get; }
public bool IsInit { set; get; }
/// <summary>
/// If path not exist, create it.
/// </summary>
/// <param name="path"></param>
/// <param name="path">please add *.db suffix or your db file suffix</param>
public override void InitSave(string path)
{
if (!Directory.Exists(path))
@ -30,13 +24,15 @@ namespace EGFramework{ @@ -30,13 +24,15 @@ namespace EGFramework{
/// <summary>
/// Init database with path.
/// </summary>
/// <param name="dataBaseName">name is the file path.</param>
/// <param name="dataBaseName">name is the file path.such as SaveData.db</param>
public void InitDatabase(string dataBaseName)
{
Connection = new SqliteConnection("Data Source="+dataBaseName+";Mode=ReadWriteCreate;"); // Open the connection:
try
{
Connection.Open();
this.Conn = dataBaseName;
IsInit = true;
}
catch (Exception ex)
{

Loading…
Cancel
Save