Files
lserp_cs_5.0/插件库/Zhaizj.Framework/ORM/Utils/CacheUtil.cs
T
2026-07-10 15:25:05 +08:00

63 lines
2.3 KiB
C#

using System;
using System.Text;
using Zhaizj.Framework;
using Zhaizj.Framework.Data;
using Zhaizj.Framework.Reflection;
using Zhaizj.Framework.ORM.Caching;
namespace Zhaizj.Framework.ORM {
internal class CacheUtil {
private static readonly ILog logger = LogManager.GetLogger( typeof( CacheUtil ) );
public static void CheckCountCache( String action, IEntity obj, EntityInfo entityInfo ) {
for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++) {
IEntity container = null;
String propertyName = null;
EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
if ((info.Name != "Id") && !(info.Name == "OID")) {
ICacheAttribute attribute = ReflectionUtil.GetAttribute( info.Property, typeof( CacheCountAttribute ) ) as ICacheAttribute;
if (attribute != null) {
container = ReflectionUtil.GetPropertyValue( obj, info.Name ) as IEntity;
propertyName = attribute.TargetPropertyName.Replace( " ", "" );
}
if (container != null) {
String columnName = Entity.GetInfo( container ).GetColumnName( propertyName );
setCountCacheBySql( container, columnName, action );
}
}
}
}
public static void setCountCacheBySql( IEntity container, String columName, String action ) {
EntityInfo et = Entity.GetInfo( container );
StringBuilder builder = new StringBuilder();
builder.Append( "update " );
builder.Append( et.TableName );
builder.Append( " set " );
builder.Append( columName );
builder.Append( "=" );
builder.Append( columName );
if (action.ToLower() == "insert") {
builder.Append( "+1" );
}
else {
builder.Append( "-1" );
}
builder.AppendFormat( " where id={0}", container.Id );
DataFactory.GetCommand( builder.ToString(), DbContext.getConnection( et ) ).ExecuteNonQuery();
logger.Debug( "set counter cache : " + builder.ToString() );
CacheTime.updateTable( et.Type );
}
}
}