OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Redis使用 (2)

OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Redis使用

Osharp启用Redis

修改appsettings.Development.json中的Redis节点

OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Redis使用

配置说明

2.1 Configuration配置连接串,因为Redis没有配置密码,只要填写localhost,如有密码,参考格式phone.qiadoo.com:6379,password=密码

2.2 InstanceName配置Redis中的前缀,名称中建议以:结尾,具体效果自己对比

2.3 Enabled配置是否启用,当然要改为true

Osharp初始化缓存查看

启动项目,进入Swagger界面

使用工具查看Redis的数据,可以看到系统初始化后加载的数据

2.1 FunctionRoles缓存,初始化代码位于项目OSharp.Permissions,命名空间OSharp.Permission.SecurityPackBase

2.2 All_KeyValue_Key缓存,初始化代码位于项目OSharp.Permissions,命名空间OSharp.Systems.SystemPack

OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Redis使用

OSharp缓存使用方法

缓存使用

// ----------------------------------------------------------------------- // <copyright file="TestController.cs" company="OSharp开源团队"> // Copyright (c) 2014-2018 OSharp. All rights reserved. // </copyright> // <site></site> // <last-editor>郭明锋</last-editor> // <last-date>2018-06-27 4:50</last-date> // ----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using CanDoo.Test.Identity; using CanDoo.Test.Identity.Dtos; using CanDoo.Test.Identity.Entities; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OSharp.AspNetCore; using OSharp.AspNetCore.Mvc; using OSharp.AspNetCore.Mvc.Filters; using OSharp.Caching; using OSharp.Collections; using OSharp.Core.Systems; using OSharp.Data; using OSharp.Dependency; using OSharp.Entity; using OSharp.Exceptions; using OSharp.Identity; using OSharp.Json; namespace CanDoo.Test.Web.Controllers { [Description("网站-缓存测试")] [ClassFilter] public class TestCacheController : ApiController { private readonly IDistributedCache _cache; public TestCacheController(IDistributedCache cache, IKeyValueStore keyValueStore) { _cache = cache; } [HttpGet] [MethodFilter] [Description("写入缓存")] public async Task<string> Write() { await _cache.SetAsync("TestWrite", "Test"); return "缓存写入完成"; } [HttpGet] [MethodFilter] [Description("读取缓存")] public async Task<string> Read() { return await _cache.GetAsync<string>("TestWrite"); } [HttpGet] [MethodFilter] [Description("移除缓存")] public async Task<string> Remove() { await _cache.RemoveAsync("TestWrite"); return "缓存移除完成"; } } }

键值对使用

// ----------------------------------------------------------------------- // <copyright file="SettingsController.cs" company="OSharp开源团队"> // Copyright (c) 2014-2018 OSharp. All rights reserved. // </copyright> // <site></site> // <last-editor>郭明锋</last-editor> // <last-date>2018-06-27 4:50</last-date> // ----------------------------------------------------------------------- using System; using System.ComponentModel; using System.Threading.Tasks; using CanDoo.Test.Systems.Dtos; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using OSharp.AspNetCore.Mvc.Filters; using OSharp.AspNetCore.UI; using OSharp.Core.Modules; using OSharp.Core.Systems; using OSharp.Data; using OSharp.Exceptions; namespace CanDoo.Test.Web.Areas.Admin.Controllers { [ModuleInfo(Order = 1, Position = "Systems", PositionName = "系统管理模块")] [Description("管理-系统设置")] public class SettingsController : AdminApiController { private readonly IKeyValueStore _keyValueStore; /// <summary> /// 初始化一个<see cref="SettingsController"/>类型的新实例 /// </summary> public SettingsController(IKeyValueStore keyValueStore) { _keyValueStore = keyValueStore; } /// <summary> /// 读取设置 /// </summary> /// <param>设置根节点,如投票设置为Votes</param> /// <returns>相应节点的设置信息</returns> [HttpGet] [ModuleInfo] [Description("读取设置")] public IActionResult Read(string root) { ISetting setting; switch (root) { case "System": setting = _keyValueStore.GetSetting<SystemSetting>(); break; default: throw new OsharpException($"未知的设置根节点: {root}"); } return Json(new SettingOutputDto(setting)); } /// <summary> /// 保存指定设置项 /// </summary> /// <param>设置信息</param> /// <returns>JSON操作结果</returns> [HttpPost] [ModuleInfo] [Description("保存设置")] [ServiceFilter(typeof(UnitOfWorkAttribute))] public async Task<AjaxResult> Update(SettingInputDto dto) { Check.NotNull(dto, nameof(dto)); Type type = Type.GetType(dto.SettingTypeName); if (type == null) { return new AjaxResult($"设置类型\"{dto.SettingTypeName}\"无法找到"); } ISetting setting = JsonConvert.DeserializeObject(dto.SettingJson, type) as ISetting; OperationResult result = await _keyValueStore.SaveSetting(setting); if (result.Succeeded) { return new AjaxResult("设置保存成功"); } return result.ToAjaxResult(); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwxswy.html