# Apteryx.StackExChange.Redis.Extend **Repository Path**: code-Institutes/Apteryx.StackExChange.Redis.Extend ## Basic Information - **Project Name**: Apteryx.StackExChange.Redis.Extend - **Description**: 针对StackExChange.Redis的扩展,加入实体(String类型)并可设置过期时间 - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2019-05-06 - **Last Updated**: 2022-02-16 ## Categories & Tags **Categories**: cache-modules **Tags**: None ## README # 针对StackExChange.Redis的扩展,加入实体(String类型)并可设置过期时间 ![示例](https://gitee.com/uploads/images/2019/0506/115209_8de1481c_1322519.png "示例") ### 安装 Install-Package Apteryx.StackexChange.Redis.Extend ### 使用方法一、 public class Account { public string Name { get; set; } } public class RedisEntity:RedisDBProvider { public RedisEntity(string conn) : base(conn) { } public IRedisCollection Accounts { get { return _database.GetCollection(); } } } static void Main(string[] args) { RedisEntity db = new RedisEntity("xxx.xx.xx.248:6379,password=xxx,defaultDatabase=1"); for (int i = 1; i <= 10; i++) { db.Accounts.AddAsync(new Account() {Name = "张三" + i}, TimeSpan.FromSeconds(20)); } db.Accounts.AddAsync("key11", new Account() {Name = "张三11"}, TimeSpan.FromSeconds(20)); var account1 = db.Accounts.Find("key11");//支持key查询 var account2 = db.Accounts.FirstOrDefault(f => f.Name == "张三8");//注意此方法为遍历对比,只适用少量数据。 } ### 使用方法二、 public class Account { public string Name { get; set; } } static void Main(string[] args) { var redisClient = ConnectionMultiplexer.Connect("xxx.xx.xx.248:6379,password=xxx,defaultDatabase=0"); var db = redisClient.GetDatabase(); using (var strlist = db.AsClientType()) { for (int i = 1; i <= 100; i++) { strlist.Append(new Account() { Name = "张三" + i }, TimeSpan.FromMinutes(1)); } } } ### 使用方法三、 public class Account { public string Name { get; set; } } public class RedisEntity : RedisDBProvider { public RedisEntity(IOptionsMonitor options) : base(options) { } public IRedisCollection Accounts => Database.GetCollection(); } public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddRedis(options => { options.ConnectionString = "xxx.xxx.xxx.xxx:6379,password = 123456,defaultDatabase = 0"; }); //............................ } } [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { private RedisEntity db = null; public ValuesController(IRedisService redis) { db = redis as RedisEntity; } // GET api/values [HttpGet()] public IActionResult Get() { for (int i = 1; i <= 10; i++) { db.Accounts.AddAsync(new Account() {Name = "张三" + i}, TimeSpan.FromSeconds(20)); } db.Accounts.AddAsync("key11", new Account() {Name = "张三11"}, TimeSpan.FromSeconds(20)); var account1 = db.Accounts.Find("key11");//支持key查询 var account2 = db.Accounts.FirstOrDefault(f => f.Name == "张三8");//注意此方法为遍历对比,只适用少量数据。 return Ok(account1); } }