FreeSqlExtensions扩展类

  • binGe博客
  • FreeSql
  • 2022-2-8 8:47:25
  • 人已阅读
简介SqlSugar有自己的内置缓存扩展,而Freesql却不考虑这些让自己搞,没法就搞一个咯,Freesql在用户体验上越走越远了...
using FreeSql;
using Jabil.Common;
using Jabil.Core;
using Jabil.Core.CacheManager;
using Jabil.Core.Configuration;
using System.Collections.Generic;

namespace Jabil.FreeSql.Extensions
{
    /// <summary>
    /// FreeSql扩展类
    /// </summary>
    public static class FreeSqlExtensions
    {
        private static object _cacheObj = new object();

        /// <summary>
        /// 简单的单表缓存迭代,按实体Name缓存
        /// 注:此扩展已经ToList()了
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="select"></param>
        /// <returns></returns>
        public static List<T> WithCache<T>(this ISelect<T> select)
        {
            ICacheService cs = ServiceHelper.GetService<ICacheService>();
            lock (_cacheObj)
            {
                var key = typeof(T).Name;
                //使用动态缓存,根据select.ToSql()来记忆是否需要缓存
                //如果ToSql结果与历史sql不同,那么就重新读库,否则就直接取缓存结果
                var cacheSql = cs.Get("freeSqlQuery_" + key);
                if (select.ToSql() == cacheSql)
                {
                    return AppSetting.UseRedis ? JsonHelper.DeserializeObject<List<T>>(cs.Get("freeSqlQueryData_" + key)) : 
                                                 cs.Get("freeSqlQueryData_" + key) as List<T>;
                }
                cs.Add("freeSqlQuery_" + key, select.ToSql());
                var queryData = select.ToList();
                cs.Add("freeSqlQueryData_" + key, queryData);
                return queryData;
            }
        }


        //基于以上的扩展,接下来需要扩展FreeSql的IInsert和IDelete、以及IUpdate接口,以支持缓存动作
        //官方未提供虚方法,这里只能用扩展的形式实现了

        //IInsert<T1> Insert<T1>(T1 source) where T1 : class;
        /// <summary>
        /// Insert自动移除缓存扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="insert"></param>
        /// <returns></returns>
        public static IInsert<T> AutoRemoveCache<T>(this IInsert<T> insert) where T : class
        {
            ICacheService cs = ServiceHelper.GetService<ICacheService>();
            lock (_cacheObj)
            {
                try
                {
                    var key = typeof(T).Name;
                    cs.Remove("freeSqlQuery_" + key);
                    cs.Remove("freeSqlQueryData__" + key);
                }
                catch { }
            }
            return insert;
        }

        //IDelete<T1> Delete<T1>(object dywhere) where T1 : class;
        /// <summary>
        /// Delete自动移除缓存扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="delete"></param>
        /// <returns></returns>
        public static IDelete<T> AutoRemoveCache<T>(this IDelete<T> delete)
        {
            ICacheService cs = ServiceHelper.GetService<ICacheService>();
            lock (_cacheObj)
            {
                try
                {
                    var key = typeof(T).Name;
                    cs.Remove("freeSqlQuery_" + key);
                    cs.Remove("freeSqlQueryData__" + key);
                }
                catch { }
            }
            return delete;
        }

        //IUpdate<T1> Update<T1>(object dywhere) where T1 : class;
        /// <summary>
        /// Update自动移除缓存扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="update"></param>
        /// <returns></returns>
        public static IUpdate<T> AutoRemoveCache<T>(this IUpdate<T> update)
        {
            ICacheService cs = ServiceHelper.GetService<ICacheService>();
            lock (_cacheObj)
            {
                try
                {
                    var key = typeof(T).Name;
                    cs.Remove("freeSqlQuery_" + key);
                    cs.Remove("freeSqlQueryData__" + key);
                }
                catch { }
            }
            return update;
        }
    

        //多表操作缓存,待实际项目开发再研究

    }
}


文章评论

评论
  • 消灭零回复
Top