码迷,mamicode.com
首页 > 系统相关 > 详细

ABP 使用cache缓存

时间:2019-07-23 18:41:42      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:linq   ace   summary   names   only   handler   ase   conf   comm   

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(2));
                    return result.access_token;
                }
            }
            return acctoken.ToString();
        }

    }
}

 

ABP 使用cache缓存

标签:linq   ace   summary   names   only   handler   ase   conf   comm   

原文地址:https://www.cnblogs.com/LmuQuan/p/11233639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!