SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.2 KiB

using SqlSugar;
7 years ago
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExtensionsDemo
{
public class CacheDemo
{
public static void Init()
7 years ago
{
//HttpRuntimeCache();
RedisCache();
7 years ago
}
private static void HttpRuntimeCache()
{
7 years ago
ICacheService myCache = new HttpRuntimeCache();//ICacheService
7 years ago
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = myCache //Setting external cache service
}
});
7 years ago
for (int i = 0; i < 10000; i++)
{
db.Queryable<Student>().Where(it => it.Id > 0).WithCache().ToList();
}
7 years ago
7 years ago
db.Queryable<Student, Student>((s1, s2) => s1.Id == s2.Id).Select(s1 => s1).WithCache().ToList();
7 years ago
db.Queryable<Student, Student>((s1, s2) => new object[] {
JoinType.Left,s1.Id==s2.Id
}).Select(s1 => s1).WithCache().ToList();
7 years ago
7 years ago
Console.WriteLine("Cache Key Count:" + myCache.GetAllKey<string>().Count());
foreach (var item in myCache.GetAllKey<string>())
{
Console.WriteLine();
Console.WriteLine(item);
Console.WriteLine();
}
7 years ago
db.Deleteable<Student>().Where(it => it.Id == 1).RemoveDataCache().ExecuteCommand();
Console.WriteLine("Cache Key Count:" + myCache.GetAllKey<string>().Count());
}
private static void RedisCache()
{
ICacheService myCache = new RedisCache("10.1.249.196");//ICacheService
7 years ago
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = myCache //Setting external cache service
}
});
db.Queryable<Student>().Where(it => it.Id > 0).WithCache(30).ToList();
7 years ago
db.Queryable<Student, Student>((s1, s2) => s1.Id == s2.Id).Select(s1 => s1).WithCache(30).ToList();
7 years ago
db.Queryable<Student, Student>((s1, s2) => new object[] {
JoinType.Left,s1.Id==s2.Id
}).Select(s1 => s1).WithCache(30).ToList();
7 years ago
Console.WriteLine("Cache Key Count:" + myCache.GetAllKey<string>().Count());
foreach (var item in myCache.GetAllKey<string>())
{
Console.WriteLine();
Console.WriteLine(item);
Console.WriteLine();
}
db.Deleteable<Student>().Where(it => it.Id == 1).RemoveDataCache().ExecuteCommand();
Console.WriteLine("Cache Key Count:" + myCache.GetAllKey<string>().Count());
}
}
}