ABP 使用cache缓存
2024-09-05 09:34:13
using Abp.Application.Services.Dto;
using Abp.Runtime.Caching;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Senparc.CO2NET.HttpUtility;
using Senparc.Weixin.CommonAPIs.ApiHandlerWapper;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Entities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks; namespace AbpProject.WeiXinMPCache
{
public class MPCacheAppService : AbpProjectAppServiceBase, IMPCacheAppService
{
/// <summary>
/// appid
/// </summary>
private readonly string AppIdSAddress = "WeiXinMP:AppID";
/// <summary>
/// appsecret
/// </summary>
private readonly string AppSecretAddress = "WeiXinMP:AppSecret";
/// <summary>
/// 获取token的路径
/// </summary>
private readonly string AccTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; private readonly ICacheManager _cacheManager;
private readonly IConfiguration _appConfiguration; public MPCacheAppService(IConfiguration appConfiguration, ICacheManager cacheManager)
{
_appConfiguration = appConfiguration;
_cacheManager = cacheManager;
} /// <summary>
/// 获取公众微信号的token
/// </summary>
/// <returns></returns>
public async Task<string> GetAccessToken()
{
var acctoken = await _cacheManager.GetCache("WeiXinMP").GetOrDefaultAsync("AccessToken");
if (acctoken == null || string.IsNullOrEmpty(acctoken.ToString()))
{
var appId = _appConfiguration[AppIdSAddress];
var appSecret = _appConfiguration[AppSecretAddress];
var url = string.Format(AccTokenUrl, appId, appSecret);
var result = HttpClientHelper<AccessTokenResult>.Get(url,new AccessTokenResult());
if (result != null)
{
await _cacheManager.GetCache("WeiXinMP").SetAsync("AccessToken", result.access_token, TimeSpan.FromHours());
return result.access_token;
}
}
return acctoken.ToString();
} }
}
最新文章
- 【PostgreSQL】PostgreSQL的安装
- 用jekyll制作高大上的网站(二)——实际应用
- The resource identified by this request is only capable of generating responses with characteristics
- easyUI validate函数【总结篇-部分转】
- 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
- selection伪元素小解
- jquery and jquery validation 常见问题解决
- SqlSever基础 datepart函数 返回这一秒已经过去了多少毫秒
- D3.js 其他选择元素方法
- DOM笔记(九):引用类型、基本包装类型和单体内置对象
- python学习笔记13(模块、包)
- oracle的sqlnet.ora , tnsnames.ora , Listener.ora 文件的作用(转)
- VS2012 快捷键 VS Resharper 设置
- html combobox select控件设置默认选项
- KVC该机制
- ##6.1 Neutron控制节点-- openstack pike
- Java Timer, TimerTask, Timer.Schedule
- java (图片转PDF)
- html学习笔记(一)
- jmeter接口测试2-断言