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

OsharpNS轻量级.net core快速开发框架简明入门教程

教程目录

  1. 从零开始启动Osharp

    1.1. 使用OsharpNS项目模板创建项目

    1.2. 配置数据库连接串并启动项目

    1.3. OsharpNS.Swagger使用实例(登录和授权)

    1.4. Angular6的前端项目启动

  2. Osharp代码生成器的使用

    2.1 生成器的使用

    2.2 生成代码详解(如何自己实现业务功能)

  3. Osharp部分模块使用

    3.1 Osharp.Redis使用

    3.2 Osharp.Hangfire使用

    3.3 Osharp.Permissions使用

  4. Osharp深度学习和使用

    4.1 切换数据库(从SqlServer改为MySql)

    4.2 多上下文配置(多个数据库的使用)

    4.3. 自定义模块的定义(Senparc.Weixin的使用)

    4.4. 继续学习中....

OsharpNS官方资源
项目地址:https://github.com/i66soft/osharp-ns20
演示地址:https://www.osharp.org 直接使用QQ登录可以查看效果
文档地址:https://docs.osharp.org 正在完善中....
发布博客:https://www.cnblogs.com/guomingfeng/p/osharpns-publish.html 大神看这个文档应该就能跑起来,从零开始启动Osharp基于此文档完成
VS生成器插件:https://marketplace.visualstudio.com/items?itemName=LiuliuSoft.osharp
官方交流QQ群:85895249

OsharpNS.Redis使用

目录

  1. 在Windows下配置Redis

  2. 使用RedisDesktopManager连接Redis

  3. Osharp启用Redis

  4. Osharp初始化缓存查看

  5. Osharp缓存使用方法

在Windows下配置Redis

  1. 前往https://github.com/MicrosoftArchive/redis/releases下载Windows版的Redis(.msi后缀),并完成安装

    点击版本号,进入对应版本的下载界面,下载.msi格式的就行

    安装时基本都是默认选项,下面这个界面的时候注意选下下面的选项

  2. Redis开启远程访问,注意修改的配置文件一般是redis安装目录下redis.windows-service.conf

    网上很多文档都说改redis.windows.conf, 改了也没用;

    其实我们可以查看一下服务里面的信息,看看到底使用的是那个配置文件,是哪个就改哪个,如下图:

      

    配置文件基本修改两个地方即可; 

      

    修改完以后,重启redis服务即可;

使用RedisDesktopManager连接Redis

  1. 下载工具,工具是收费的,我是在https://www.newasp.net/soft/391754.html下载的,病毒自己鉴别,也可以自己找链接,反正下载安装了就行

  2. 配置连接

Osharp启用Redis

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

  1. 配置说明

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

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

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

Osharp初始化缓存查看

  1. 启动项目,进入Swagger界面

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

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

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

OSharp缓存使用方法

  1. 缓存使用
   // -----------------------------------------------------------------------
   //  <copyright file="TestController.cs" company="OSharp开源团队">
   //      Copyright (c) 2014-2018 OSharp. All rights reserved.
   //  </copyright>
   //  <site>http://www.osharp.org</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 "缓存移除完成";
           }
       }

   }

  1. 键值对使用

   // -----------------------------------------------------------------------
   //  <copyright file="SettingsController.cs" company="OSharp开源团队">
   //      Copyright (c) 2014-2018 OSharp. All rights reserved.
   //  </copyright>
   //  <site>http://www.osharp.org</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 name="root">设置根节点,如投票设置为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 name="dto">设置信息</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.cnblogs.com/candoo/p/10790121.html