效率低下的EhCache性能

效率低下的EhCache性能

本文介绍了效率低下的EhCache性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JPA属性

  props.put(hibernate.cache.use_query_cache,true); 
props.put(hibernate.cache.use_second_level_cache,true);
props.put(hibernate.temp.use_jdbc_metadata_defaults,false);
props.put(hibernate.cache.region.factory_class,org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory);
props.put(javax.persistence.sharedCache.mode,SharedCacheMode.ALL);

Ehcache对于相同的查询效率不高,

  private int generateHashCode(){
int result = 13;
result = 37 * result +(firstRow == null?0:firstRow.hashCode());
result = 37 * result +(maxRows == null?0:maxRows.hashCode());
for(int i = 0; i< positionsParameterValues.length; i ++){
result = 37 * result +(locationsParameterValues [i] == null?0:positions PositionParameterTypes [i] .getHashCode(positions PositionParameterValues [一世] ) );
}
result = 37 * result +(namedParameters == null?0:namedParameters.hashCode());
result = 37 * result +(filterKeys == null?0:filterKeys.hashCode());
result = 37 * result +(customTransformer == null?0:customTransformer.hashCode());
result = 37 * result +(tenantIdentifier == null?0:tenantIdentifier.hashCode());
result = 37 * result + sqlQueryString.hashCode();
返回结果;
}

这与Class

相关

  org.hibernate.type.AbstractType 

public int getHashCode(Object x){
return x.hashCode();
}

它为同一个Array对象生成一个不同的(新的)hachCode [01, 1]!

这个hashCode方法应该是数组递归的

解决方案 div>

递归版本完整工作

类org.hibernate.type.AbstractType

  public int getHashCode(Object x){
if(x instanceof Object []){
int result = 1;
for(Object element:(Object [])x)
result = 31 * result +(element == null?0:getHashCode(element));
返回结果;
}
return x.hashCode();

AND



<$ p
$ {$ b $ if(a == a2)
return true;
if(a == null || a2 == null)
return false;

int length = a.length;
if(a2.length!= length)
return false;

for(int i = 0; i< length; i ++){
Object o1 = a [i];
Object o2 = a2 [i];
if(o1 == null){
if(o2!= null)
return false;
} else {
if(o2 == null)
return false;
if(o1 instanceof Object []){
if(!(o2 instanceof Object []))
return false;
else
if(!arraysEquals((Object [])o1,(Object [])o2))
return false;
} else
if(!o1.equals(o2))
return false;
}
}
返回true;
}
public static boolean equals(final Object x,final Object y){
if(x!= null&& x instanceof Object []&& y!= null && y instanceof Object [])
return arraysEquals((Object [])x,(Object [])y);
return x == y || (x!= null&& y!= null&& x.equals(y));
}


Using thoses JPA properties

props.put( "hibernate.cache.use_query_cache", "true" );
props.put( "hibernate.cache.use_second_level_cache", "true" );
props.put("hibernate.temp.use_jdbc_metadata_defaults", "false");
props.put( "hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" );
props.put( "javax.persistence.sharedCache.mode", SharedCacheMode.ALL );

Ehcache is not efficient for the same query,

Problem is related to function namedParameters.hashCode() of QueryCache class, it generates a different HashCode for the same query !

private int generateHashCode() {
        int result = 13;
        result = 37 * result + ( firstRow==null ? 0 : firstRow.hashCode() );
        result = 37 * result + ( maxRows==null ? 0 : maxRows.hashCode() );
        for ( int i=0; i< positionalParameterValues.length; i++ ) {
            result = 37 * result + ( positionalParameterValues[i]==null ? 0 : positionalParameterTypes[i].getHashCode( positionalParameterValues[i] ) );
        }
        result = 37 * result + ( namedParameters==null ? 0 : namedParameters.hashCode() );
        result = 37 * result + ( filterKeys ==null ? 0 : filterKeys.hashCode() );
        result = 37 * result + ( customTransformer==null ? 0 : customTransformer.hashCode() );
        result = 37 * result + ( tenantIdentifier==null ? 0 : tenantIdentifier.hashCode() );
        result = 37 * result + sqlQueryString.hashCode();
        return result;
}

which is related to the Class

org.hibernate.type.AbstractType

public int getHashCode(Object x) {
    return x.hashCode();
}

it generates a different (new) hachCode for the same Array object [01, 1] !

This hashCode method should be recursive for Arrays

解决方案

Recursive version Full working

Class org.hibernate.type.AbstractType

public int getHashCode(Object x) {
        if (x instanceof Object[]){
            int result = 1;
            for (Object element : (Object[]) x)
                result = 31 * result + (element == null ? 0 : getHashCode(element));
            return result;
        }
        return x.hashCode();
    }

AND

public static boolean arraysEquals(Object[] a, Object[] a2) {
            if (a==a2)
                return true;
            if (a==null || a2==null)
                return false;

            int length = a.length;
            if (a2.length != length)
                return false;

            for (int i=0; i<length; i++) {
                Object o1 = a[i];
                Object o2 = a2[i];
                if (o1==null){
                    if (o2!=null)
                        return false;
                }else{
                    if (o2==null)
                        return false;
                    if (o1 instanceof Object[]){
                        if (!(o2 instanceof Object[]))
                            return false;
                        else
                            if (!arraysEquals( (Object[]) o1, (Object[]) o2))
                                return false;
                    }else
                        if (!o1.equals(o2))
                            return false;
                }
            }
            return true;
    }
    public static boolean equals(final Object x, final Object y) {
        if (x!=null && x instanceof Object[] && y!=null && y instanceof Object[] )
            return arraysEquals((Object[])x, (Object[])y);
        return x == y || ( x != null && y != null && x.equals( y ) );
    }

这篇关于效率低下的EhCache性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:32