init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using Zhaizj.Framework.Caching;
|
||||
using Zhaizj.Framework.Data;
|
||||
using Zhaizj.Framework.ORM.Operation;
|
||||
using Zhaizj.Framework.ORM.Caching;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
/// <summary>
|
||||
/// 二级缓存(application级)的缓存池
|
||||
/// </summary>
|
||||
internal class ApplicationPool : IObjectPool {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( ApplicationPool ) );
|
||||
private static IApplicationCache appCache = CacheManager.GetApplicationCache();
|
||||
|
||||
public static ApplicationPool Instance = new ApplicationPool();
|
||||
|
||||
private ApplicationPool() { }
|
||||
|
||||
// 只是单纯加入缓存,比如在findList的时候,顺便加入缓存
|
||||
// 插入数据的时候只更新表的timestamp,并不加入缓存
|
||||
public void Add( IEntity obj ) {
|
||||
|
||||
if (obj == null) return;
|
||||
|
||||
String key = CacheKey.getObject( obj.GetType(), obj.Id );
|
||||
addToApplication( key, obj );
|
||||
CacheTime.updateObject( obj ); // 加入缓存的时候,设置最新 timestamp
|
||||
}
|
||||
|
||||
public void AddAll( Type t, IList objList ) {
|
||||
String key = CacheKey.getObjectAll( t );
|
||||
addList( key, objList );
|
||||
}
|
||||
|
||||
public void AddQueryList( Type t, String sql, Dictionary<String, Object> parameters, IList objList ) {
|
||||
String key = CacheKey.getQuery( t, sql, parameters );
|
||||
addList( key, objList );
|
||||
}
|
||||
|
||||
public void AddPage( Type t, String condition, ObjectPage pager, IList list ) {
|
||||
String key = CacheKey.getPageList( t, condition, pager.getSize(), pager.getCurrent() );
|
||||
addList( key, list );
|
||||
addToApplication( CacheKey.getPagerInfoKey( key ), pager ); // 添加分页统计数据
|
||||
}
|
||||
|
||||
public void AddSqlList( String sql, IList objList ) {
|
||||
if (objList == null) return;
|
||||
addList( sql, objList );
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------
|
||||
|
||||
public void AddCount( Type t, int count ) {
|
||||
String key = CacheKey.getCountKey( t );
|
||||
addToApplication( key, count );
|
||||
CacheTime.updateCount( t );
|
||||
}
|
||||
|
||||
public void AddCount( Type t, String condition, int count ) {
|
||||
String key = CacheKey.getCountKey( t, condition );
|
||||
addToApplication( key, count );
|
||||
CacheTime.updateCount( t, condition );
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
|
||||
|
||||
private static void addToApplication( String key, Object val ) {
|
||||
|
||||
int minutes = DbConfig.Instance.ApplicationCacheMinutes;
|
||||
|
||||
if (minutes == CacheTime.CacheForever) {
|
||||
appCache.Put( key, val );
|
||||
logger.Debug( "APP_cache_+=>" + key );
|
||||
}
|
||||
else if (minutes > 0) {
|
||||
appCache.Put( key, val, minutes );
|
||||
logger.Debug( "APP_cache_+=>" + key );
|
||||
}
|
||||
}
|
||||
|
||||
private static void addList( String key, IList objList ) {
|
||||
|
||||
if (objList == null) return;
|
||||
|
||||
foreach (IEntity obj in objList) {
|
||||
addToApplication( CacheKey.getObject( obj.GetType(), obj.Id ), obj );
|
||||
CacheTime.updateObject( obj ); // 加入缓存的时候,设置最新 timestamp
|
||||
}
|
||||
|
||||
List<int> ids = new List<int>();
|
||||
foreach (IEntity obj in objList) ids.Add( obj.Id );
|
||||
addToApplication( key, ids );
|
||||
CacheTime.updateList( key );
|
||||
}
|
||||
|
||||
//-------------------------------------------------------查询-----------------------------------------------------------
|
||||
|
||||
// 内部调用,需要查询一级缓存
|
||||
private IEntity findOnePrivate( Type t, int id ) {
|
||||
|
||||
String key = CacheKey.getObject( t, id );
|
||||
IEntity result = ContextPool.Instance.FindOne( t, id );
|
||||
if( result != null ) return result;
|
||||
|
||||
result = this.FindOne( t, id );
|
||||
ContextPool.Instance.Add( result );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public IEntity FindOne( Type t, int id ) {
|
||||
|
||||
String key = CacheKey.getObject( t, id );
|
||||
|
||||
IEntity result = getFromApplication( key ) as IEntity;
|
||||
if (result != null) {
|
||||
if (CacheTime.isObjectUpdated( result )) return null; // 检查是否更新
|
||||
logger.Debug( "APP_cache_get=>" + key );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public IList FindAll( Type t ) {
|
||||
|
||||
return getListFromCache( CacheKey.getObjectAll( t ), t );
|
||||
}
|
||||
|
||||
public IList FindByQuery( ConditionInfo condition ) {
|
||||
return getListFromCache( CacheKey.getQuery( condition.Type, condition.Sql, condition.Parameters ), condition.Type );
|
||||
}
|
||||
|
||||
public IList FindByQuery( Type type, String _queryString, Dictionary<String, Object> _namedParameters ) {
|
||||
return getListFromCache( CacheKey.getQuery( type, _queryString, _namedParameters ), type );
|
||||
}
|
||||
|
||||
public IPageList FindPage( Type t, String condition, ObjectPage pager ) {
|
||||
String key = CacheKey.getPageList( t, condition, pager.getSize(), pager.getCurrent() );
|
||||
logger.Debug( "FindPage=>" + t.FullName );
|
||||
IList list = getListFromCache( key, t );
|
||||
if (list == null) return null;
|
||||
|
||||
IPageList result = new PageList();
|
||||
|
||||
ObjectPage pageInfo = getPagerInfo( key );
|
||||
if (pageInfo == null) return null;
|
||||
|
||||
result.Results = list;
|
||||
result.PageCount = pageInfo.PageCount;
|
||||
result.RecordCount = pageInfo.RecordCount;
|
||||
result.Size = pageInfo.getSize();
|
||||
result.PageBar = pageInfo.PageBar;
|
||||
result.Current = pageInfo.getCurrent();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ObjectPage getPagerInfo( String pageKey ) {
|
||||
return getFromApplication( CacheKey.getPagerInfoKey( pageKey ) ) as ObjectPage;
|
||||
}
|
||||
|
||||
public IList FindBySql( String sql, Type t ) {
|
||||
return getListFromCache( sql, t );
|
||||
}
|
||||
|
||||
public int FindCount( Type t ) {
|
||||
if (CacheTime.isCountUpdate( t )) return -1;
|
||||
String key = CacheKey.getCountKey( t );
|
||||
Object obj = getFromApplication( key );
|
||||
return obj == null ? -1 : (int)obj;
|
||||
}
|
||||
|
||||
public int FindCount( Type t, String condition ) {
|
||||
if (CacheTime.isCountUpdate( t, condition )) return -1;
|
||||
String key = CacheKey.getCountKey( t, condition );
|
||||
Object obj = getFromApplication( key );
|
||||
return obj == null ? -1 : (int)obj;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------
|
||||
|
||||
public IList getListFromCache( String queryKey, Type t ) {
|
||||
|
||||
if (CacheTime.isListUpdate( queryKey, t )) return null;
|
||||
|
||||
List<int> ids = appCache.Get( queryKey ) as List<int>;
|
||||
if (ids == null) return null;
|
||||
|
||||
IList result = new ArrayList();
|
||||
foreach (int id in ids) {
|
||||
|
||||
IEntity obj = this.findOnePrivate( t, id );
|
||||
|
||||
if (obj == null) return null;
|
||||
|
||||
if (CacheTime.isObjectUpdated( obj )) return null; // 如果有任何一个对象更新过,则缓存失效
|
||||
|
||||
// 检查实体属性
|
||||
renewEntityPropertyValue( obj );
|
||||
|
||||
result.Add( obj );
|
||||
}
|
||||
|
||||
|
||||
logger.Debug( "APP_cache_get=>" + queryKey );
|
||||
return result;
|
||||
}
|
||||
|
||||
private void renewEntityPropertyValue( IEntity obj ) {
|
||||
EntityInfo ei = Entity.GetInfo( obj );
|
||||
foreach (EntityPropertyInfo ep in ei.EntityPropertyList) {
|
||||
IEntity objP = obj.get( ep.Name ) as IEntity;
|
||||
if (objP == null) continue;
|
||||
|
||||
IEntity val = this.findOnePrivate( objP.GetType(), objP.Id );
|
||||
if (val == null) continue;
|
||||
|
||||
ep.SetValue( obj, val );
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getFromApplication( String key ) {
|
||||
return appCache.Get( key );
|
||||
}
|
||||
|
||||
public void Delete( Type t, int id ) {
|
||||
appCache.Remove( CacheKey.getObject( t.FullName, id ) );
|
||||
logger.Debug( "Delete=>" + t.FullName + id );
|
||||
CacheTime.updateTable( t );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
|
||||
internal class CacheKey {
|
||||
|
||||
public static String getObject( Type t, int id ) {
|
||||
return getObject( t.FullName, id );
|
||||
}
|
||||
|
||||
public static String getObject( String typeFullName, int id ) {
|
||||
return "__object_" + typeFullName + "_" + id.ToString();
|
||||
}
|
||||
|
||||
public static String getObjectAll( Type t ) {
|
||||
return "__objectAll_" + t.FullName + "_all";
|
||||
}
|
||||
|
||||
public static String getQuery( Type t, String sql, Dictionary<String, Object> parameters ) {
|
||||
String str = sql + " paramValue : ";
|
||||
foreach (KeyValuePair<String, Object> pair in parameters) {
|
||||
str = str + " \"" + cvt.ToNotNull( pair.Value ) + "\", ";
|
||||
}
|
||||
|
||||
return "__objectQueryList_" + t.FullName + "_" + str.Trim().TrimEnd( ',' );
|
||||
}
|
||||
|
||||
public static string getPageList( Type t, string condition, int pageSize, int current ) {
|
||||
return "__objectPageList_" + t.FullName + "_ids_" + condition + "_pagesize" + pageSize + "_" + current;
|
||||
}
|
||||
|
||||
|
||||
public static String getPagerInfoKey( String key ) {
|
||||
return "__objectPagebar_" + key;
|
||||
}
|
||||
|
||||
public static string getCountKey( Type t ) {
|
||||
return "__objectCount_" + t.FullName;
|
||||
}
|
||||
|
||||
|
||||
public static string getCountKey( Type t, string condition ) {
|
||||
return "__objectCount_" + t.FullName + "_" + condition;
|
||||
}
|
||||
|
||||
public static String getSqlKey( String sql ) {
|
||||
return "__objectSql_"+ sql;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using Zhaizj.Framework.Caching;
|
||||
using Zhaizj.Framework.ORM.Caching;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
/// <summary>
|
||||
/// 对象的缓存时间管理工具
|
||||
/// </summary>
|
||||
public class CacheTime {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( CacheTime ) );
|
||||
private static IApplicationCache appCache = CacheManager.GetApplicationCache();
|
||||
|
||||
public const int CacheForever = -999;
|
||||
|
||||
private static Hashtable map = new Hashtable();
|
||||
|
||||
public static void updateTable( Type t ) {
|
||||
logger.Debug( "updateTable at=>" + DateTime.Now + "_" + t.FullName );
|
||||
map[TimestampKey.getTable( t.FullName )] = DateTime.Now;
|
||||
}
|
||||
|
||||
private static Object getTableUpdated( string typeFullName ) {
|
||||
return map[TimestampKey.getTable( typeFullName )];
|
||||
}
|
||||
|
||||
private static void updatePrivate( String key, DateTime t) {
|
||||
//appCache.Put( key, t );
|
||||
map[key] = t;
|
||||
}
|
||||
|
||||
private static object getUpdatedPrivate( String key ) {
|
||||
//return appCache.Get( key );
|
||||
return map[key];
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
public static void updateObject( IEntity obj ) {
|
||||
updatePrivate( TimestampKey.getObject( obj ), DateTime.Now );
|
||||
}
|
||||
|
||||
private static Object getObjectUpdated( IEntity obj ) {
|
||||
return getUpdatedPrivate( TimestampKey.getObject( obj ) );
|
||||
}
|
||||
|
||||
public static Boolean isObjectUpdated( IEntity obj ) {
|
||||
|
||||
Object tblUpdated = getTableUpdated( obj.GetType().FullName );
|
||||
if (tblUpdated == null) return false;
|
||||
|
||||
Object objUpdated = getObjectUpdated( obj );
|
||||
if (objUpdated == null) return true;
|
||||
|
||||
return (DateTime)tblUpdated >= (DateTime)objUpdated;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
public static void updateList( String cacheKey ) {
|
||||
logger.Debug( "updateList at=>" + DateTime.Now + "_" + cacheKey );
|
||||
updatePrivate( TimestampKey.getList( cacheKey ), DateTime.Now );
|
||||
}
|
||||
|
||||
private static Object getListUpdaed( String cacheKey ) {
|
||||
return getUpdatedPrivate( TimestampKey.getList( cacheKey ) );
|
||||
}
|
||||
|
||||
public static bool isListUpdate( string cacheKey, Type t ) {
|
||||
|
||||
Object tblUpdated = getTableUpdated( t.FullName );
|
||||
if (tblUpdated == null) return false;
|
||||
logger.Debug( "tblUpdated=" + tblUpdated );
|
||||
|
||||
Object listUpdated = getListUpdaed( cacheKey );
|
||||
if (listUpdated == null) return true;
|
||||
logger.Debug( "listUpdated=" + listUpdated );
|
||||
|
||||
return (DateTime)tblUpdated >= (DateTime)listUpdated;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
public static void updateCount( Type t ) {
|
||||
map[TimestampKey.getCount( t )] = DateTime.Now;
|
||||
}
|
||||
|
||||
public static void updateCount( Type t, String condition ) {
|
||||
map[TimestampKey.getCount( t, condition )] = DateTime.Now;
|
||||
}
|
||||
|
||||
private static Object getCountUpdated( Type t ) {
|
||||
return map[TimestampKey.getCount( t )];
|
||||
}
|
||||
|
||||
private static Object getCountUpdated( Type t, String condition ) {
|
||||
return map[TimestampKey.getCount( t, condition )];
|
||||
}
|
||||
|
||||
public static Boolean isCountUpdate( Type t ) {
|
||||
|
||||
Object tblUpdated = getTableUpdated( t.FullName );
|
||||
if (tblUpdated == null) return false;
|
||||
|
||||
Object countUpdated = getCountUpdated( t );
|
||||
if (countUpdated == null) return true;
|
||||
|
||||
return (DateTime)tblUpdated >= (DateTime)countUpdated;
|
||||
}
|
||||
|
||||
public static Boolean isCountUpdate( Type t, String condition ) {
|
||||
|
||||
Object tblUpdated = getTableUpdated( t.FullName );
|
||||
if (tblUpdated == null) return false;
|
||||
|
||||
Object countUpdated = getCountUpdated( t, condition );
|
||||
if (countUpdated == null) return true;
|
||||
|
||||
return (DateTime)tblUpdated >= (DateTime)countUpdated;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Web;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.Data;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
/// <summary>
|
||||
/// Ò»¼¶»º´æ(ÉÏÏÂÎÄ»º´æ)
|
||||
/// </summary>
|
||||
public class ContextCache {
|
||||
|
||||
public static Object Get( String objectKey ) {
|
||||
if (objectKey == null) {
|
||||
throw new Exception( "get context cache : objectKey is empty" );
|
||||
}
|
||||
return Map[objectKey];
|
||||
}
|
||||
|
||||
public static void Put( String objectKey, Object objectValue ) {
|
||||
if (strUtil.IsNullOrEmpty( objectKey )) {
|
||||
throw new Exception( "get context cache : objectKey is empty" );
|
||||
}
|
||||
Map[objectKey] = objectValue;
|
||||
}
|
||||
|
||||
public static void Remove( String objectKey ) {
|
||||
if (!strUtil.IsNullOrEmpty( objectKey )) {
|
||||
Map.Remove( objectKey );
|
||||
}
|
||||
}
|
||||
|
||||
public static void Remove( String typeFullName, int id ) {
|
||||
String objectKey = CacheKey.getObject( typeFullName, id );
|
||||
Remove( objectKey );
|
||||
}
|
||||
|
||||
public static void Clear() {
|
||||
Map.Clear();
|
||||
}
|
||||
|
||||
private static IDictionary Map {
|
||||
get { return DbContext.getContextCache(); }
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Data;
|
||||
using System.Collections;
|
||||
using Zhaizj.Framework.ORM.Operation;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
/// <summary>
|
||||
/// 一级缓存(上下文缓存)的缓存池
|
||||
/// </summary>
|
||||
internal class ContextPool : IObjectPool {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( ContextPool ) );
|
||||
|
||||
public static ContextPool Instance = new ContextPool();
|
||||
|
||||
private ContextPool() { }
|
||||
|
||||
public void Add( IEntity obj ) {
|
||||
|
||||
if (obj == null) return;
|
||||
|
||||
String key = CacheKey.getObject( obj.GetType(), obj.Id );
|
||||
addToContext( key, obj );
|
||||
CacheTime.updateObject( obj ); // 加入缓存的时候,设置最新 timestamp
|
||||
}
|
||||
|
||||
|
||||
public void AddAll( Type t, IList objList ) {
|
||||
addList( CacheKey.getObjectAll( t ), objList );
|
||||
}
|
||||
|
||||
public void AddQueryList( Type t, String sql, Dictionary<String, Object> parameters, IList objList ) {
|
||||
addList( CacheKey.getQuery( t, sql, parameters ), objList );
|
||||
}
|
||||
|
||||
public void AddPage( Type t, String condition, ObjectPage pager, IList list ) {
|
||||
String key = CacheKey.getPageList( t, condition, pager.getSize(), pager.getCurrent() );
|
||||
addList( key, list );
|
||||
}
|
||||
|
||||
public void AddCount( Type t, int count ) {
|
||||
String key = CacheKey.getCountKey( t );
|
||||
addToContext( key, count );
|
||||
}
|
||||
|
||||
public void AddCount( Type t, String condition, int count ) {
|
||||
String key = CacheKey.getCountKey( t, condition );
|
||||
addToContext( key, count );
|
||||
}
|
||||
|
||||
public void AddSqlList( String sql, IList objList ) {
|
||||
addToContext( sql, objList );
|
||||
}
|
||||
|
||||
//---------------------------------------------------
|
||||
|
||||
private void addToContext( String key, Object val ) {
|
||||
|
||||
ContextCache.Put( key, val );
|
||||
logger.Debug( "ctx_cache_+=>" + key );
|
||||
}
|
||||
|
||||
private void addList( String key, IList objList ) {
|
||||
|
||||
if (objList == null ) return;
|
||||
addToContext( key, objList );
|
||||
foreach (IEntity obj in objList) {
|
||||
ContextCache.Put( CacheKey.getObject( obj.GetType(), obj.Id ), obj );
|
||||
}
|
||||
CacheTime.updateList( key );
|
||||
}
|
||||
|
||||
//-------------------------------------------------------查询-----------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public IEntity FindOne( Type t, int id ) {
|
||||
String key = CacheKey.getObject( t, id );
|
||||
return getFromContext( key ) as IEntity;
|
||||
}
|
||||
|
||||
public IList FindAll( Type t ) {
|
||||
String key = CacheKey.getObjectAll( t );
|
||||
return getListFromCache( key, t );
|
||||
}
|
||||
|
||||
public IList FindByQuery( ConditionInfo condition ) {
|
||||
return getListFromCache( CacheKey.getQuery( condition.Type, condition.Sql, condition.Parameters ), condition.Type );
|
||||
}
|
||||
|
||||
public IList FindByQuery( Type type, String _queryString, Dictionary<String, Object> _namedParameters ) {
|
||||
return getListFromCache( CacheKey.getQuery( type, _queryString, _namedParameters ), type );
|
||||
}
|
||||
|
||||
public IPageList FindPage( Type t, String condition, ObjectPage pager ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public IList FindBySql( String sql, Type t ) {
|
||||
return ContextCache.Get( sql ) as IList;
|
||||
}
|
||||
|
||||
public int FindCount( Type t ) {
|
||||
String key = CacheKey.getCountKey( t );
|
||||
Object obj = getFromContext( key );
|
||||
return obj == null ? -1 : (int)obj;
|
||||
}
|
||||
|
||||
public int FindCount( Type t, String condition ) {
|
||||
String key = CacheKey.getCountKey( t, condition );
|
||||
Object obj = getFromContext( key );
|
||||
return obj == null ? -1 : (int)obj;
|
||||
}
|
||||
|
||||
//---------------------------------------------------
|
||||
|
||||
private static Object getFromContext( String key ) {
|
||||
Object obj = ContextCache.Get( key );
|
||||
if (obj != null) {
|
||||
logger.Debug( "ctx_cache_get=>" + key );
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
private static IList getListFromCache( String queryKey, Type t ) {
|
||||
|
||||
if (CacheTime.isListUpdate( queryKey, t )) return null;
|
||||
|
||||
return getFromContext( queryKey ) as IList;
|
||||
}
|
||||
|
||||
public void Delete( Type t, int id ) {
|
||||
|
||||
ContextCache.Remove( t.FullName, id );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using Zhaizj.Framework.ORM.Operation;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
internal interface IObjectPool {
|
||||
|
||||
void Add( IEntity obj );
|
||||
void AddAll( Type t, IList objList );
|
||||
void AddQueryList( Type t, String sql, Dictionary<String, Object> parameters, IList objList );
|
||||
void AddPage( Type t, String condition, ObjectPage pager, IList list );
|
||||
void AddCount( Type t, int count );
|
||||
void AddCount( Type t, String condition, int count );
|
||||
void AddSqlList( String sql, IList objList );
|
||||
|
||||
IEntity FindOne( Type t, int id );
|
||||
IList FindAll( Type t );
|
||||
IList FindByQuery( ConditionInfo condition );
|
||||
IList FindByQuery( Type type, String _queryString, Dictionary<String, Object> _namedParameters );
|
||||
IPageList FindPage( Type t, String condition, ObjectPage pager );
|
||||
IList FindBySql( String sql, Type t );
|
||||
int FindCount( Type t );
|
||||
int FindCount( Type t, String condition );
|
||||
|
||||
void Delete( Type t, int id );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Zhaizj.Framework.Data;
|
||||
using Zhaizj.Framework.ORM.Operation;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
internal class ObjectPool {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( ObjectPool ) );
|
||||
|
||||
private static List<IObjectPool> pools = getPools();
|
||||
|
||||
private static List<IObjectPool> getPools() {
|
||||
|
||||
List<IObjectPool> list = new List<IObjectPool>();
|
||||
|
||||
if (DbConfig.Instance.ContextCache) list.Add( ContextPool.Instance );
|
||||
if (DbConfig.Instance.ApplicationCache) list.Add( ApplicationPool.Instance );
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void Add( IEntity obj ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.Add( obj );
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddAll( Type t, IList objList ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.AddAll( t, objList );
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddQueryList( Type t, String sql, Dictionary<String, Object> parameters, IList objList ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.AddQueryList( t, sql, parameters, objList );
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddPage( Type t, String condition, ObjectPage pager, IList list ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.AddPage( t, condition, pager, list );
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddCount( Type t, int count ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.AddCount( t, count );
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddCount( Type t, String condition, int count ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.AddCount( t, condition, count );
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddSqlList( String sql, IList objList ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.AddSqlList( CacheKey.getSqlKey( sql ), objList );
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------²éѯ-----------------------------------------------------------
|
||||
|
||||
|
||||
public static IEntity FindOne( Type t, int id ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
IEntity result = pool.FindOne( t, id );
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IList FindAll( Type t ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
IList result = pool.FindAll( t );
|
||||
if (result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IList FindByQuery( ConditionInfo condition ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
IList result = pool.FindByQuery( condition );
|
||||
if (result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IList FindByQuery( Type type, String _queryString, Dictionary<String, Object> _namedParameters ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
IList result = pool.FindByQuery( type, _queryString, _namedParameters );
|
||||
if (result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IPageList FindPage( Type t, String condition, ObjectPage pager ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
IPageList result = pool.FindPage( t, condition, pager );
|
||||
if (result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IList FindBySql( String sql, Type t ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
IList result = pool.FindBySql( sql, t );
|
||||
if (result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int FindCount( Type t ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
int result = pool.FindCount( t );
|
||||
if (result >= 0) return result;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int FindCount( Type t, String condition ) {
|
||||
|
||||
foreach (IObjectPool pool in pools) {
|
||||
int result = pool.FindCount( t, condition );
|
||||
if (result >= 0) return result;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
|
||||
public static void Update( IEntity obj ) {
|
||||
Add( obj );
|
||||
}
|
||||
|
||||
public static int Delete( IEntity obj ) {
|
||||
return Delete( obj.GetType(), obj.Id );
|
||||
}
|
||||
|
||||
public static int Delete( Type t, int id ) {
|
||||
foreach (IObjectPool pool in pools) {
|
||||
pool.Delete( t, id );
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2010 www.Zhaizj.Framework.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Caching {
|
||||
|
||||
internal class TimestampKey {
|
||||
|
||||
public static String getTable( string typeFullName ) {
|
||||
return "timestamp_table_" + typeFullName;
|
||||
}
|
||||
|
||||
public static string getObject( IEntity obj ) {
|
||||
return "timestamp_object_" + obj.GetType().FullName + "_" + obj.Id;
|
||||
}
|
||||
|
||||
public static string getList( string sqlCacheKey ) {
|
||||
return "timestamp_list_" + sqlCacheKey;
|
||||
}
|
||||
|
||||
public static string getCount( Type t ) {
|
||||
return "timestamp_count_" + t.FullName;
|
||||
}
|
||||
|
||||
public static string getCount( Type t, String condition ) {
|
||||
return "timestamp_count_" + t.FullName + "_" + condition;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user