Git Product home page Git Product logo

core-redis's Introduction

简介

基于StackExchange.Redis封装的Redis客户端

  • 支持.Net Framework.Net Core
  • 支持同步、异步操作(async\await)
  • 使用简单,方便扩展
  • 常见redis数据类型
    • String:字符串
    • List:列表
    • Set:集合
    • SortedSet:有序集合
    • Hash:哈希

Packages

Package NuGet Stable NuGet Pre-release Downloads MyGet
Sean.Core.Redis Sean.Core.Redis Sean.Core.Redis Sean.Core.Redis Sean.Core.Redis MyGet

Nuget包引用

Id:Sean.Core.Redis

  • Package Manager
PM> Install-Package Sean.Core.Redis

Redis配置示例

如果Redis是集群部署的话,地址用","分隔即可。

  • .NET Framework:app.configweb.config
<appSettings>
    <add key="RedisEndPoints" value="127.0.0.1:6379" />
    <add key="RedisPassword" value="" />
    <add key="RedisDefaultSerializeType" value="0" />
</appSettings>
  • .NET Core:appsettings.json
{
  "Redis": {
    "EndPoints": "127.0.0.1:6379",
    "Password": "",
    "DefaultSerializeType": 0,
  }
}

使用示例

项目:examples\Example.NetCore

  1. Redis初始化
  • 不使用依赖注入
// 示例1:
RedisManager.Initialize(configuration);

// 示例2:
RedisManager.Initialize(new RedisClientOptions
{
  EndPoints = "127.0.0.1:6379",
  Password = string.Empty,
  DefaultSerializeType = SerializeType.Json
});
  • 使用依赖注入
services.AddRedis(configuration);
  1. Redis使用示例
  • string => 最简单的用法
var cacheKey = "test";
Console.WriteLine($"添加string缓存:{RedisHelper.StringSet(cacheKey, new TestModel { Id = 1001, Name = "Sean" }, TimeSpan.FromSeconds(20))}");
Console.WriteLine($"同步获取缓存:{JsonHelper.Serialize(RedisHelper.StringGet<Test>(cacheKey))}");
Console.WriteLine($"异步获取缓存:{JsonHelper.Serialize(RedisHelper.StringGetAsync<Test>(cacheKey).Result)}");
Console.WriteLine($"手动删除缓存:{RedisHelper.KeyDelete(cacheKey)}");
  • list => 实现队列(先进先出)
var cacheKeyListQueue = "testListQueue";
var list = new List<Test>
{
    new TestModel { Id = 1002, Name = "aaa" },
    new TestModel { Id = 1003, Name = "bbb" },
    new TestModel { Id = 1004, Name = "ccc" },
};
Console.WriteLine($"添加list缓存:{RedisHelper.ListRightPush(cacheKeyListQueue, list)}");
Console.WriteLine($"设置缓存超时时间:{RedisHelper.KeyExpire(cacheKeyListQueue, TimeSpan.FromSeconds(20))}");
Console.WriteLine($"获取缓存:{JsonHelper.Serialize(RedisHelper.ListLeftPop<Test>(cacheKeyListQueue))}");
Console.WriteLine($"获取缓存:{JsonHelper.Serialize(RedisHelper.ListLeftPop<Test>(cacheKeyListQueue))}");
Console.WriteLine($"获取缓存:{JsonHelper.Serialize(RedisHelper.ListLeftPop<Test>(cacheKeyListQueue))}");
  • list => 实现堆栈(先进后出)
var cacheKeyListStack = "testListStack";
Console.WriteLine($"添加list缓存:{RedisHelper.ListLeftPush(cacheKeyListStack, list)}");
Console.WriteLine($"设置缓存超时时间:{RedisHelper.KeyExpire(cacheKeyListStack, TimeSpan.FromSeconds(20))}");
Console.WriteLine($"获取缓存:{JsonHelper.Serialize(RedisHelper.ListLeftPop<Test>(cacheKeyListStack))}");
Console.WriteLine($"获取缓存:{JsonHelper.Serialize(RedisHelper.ListLeftPop<Test>(cacheKeyListStack))}");
Console.WriteLine($"获取缓存:{JsonHelper.Serialize(RedisHelper.ListLeftPop<Test>(cacheKeyListStack))}");

core-redis's People

Contributors

sean-lu avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.