0

在 hibernate.cfg.xml 中定义所有 memcached 属性后,如何在我的代码中创建 memcachedclient?

Hibernate.cfg.xml:-

<property name="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.use_query_cache">false</property>
        <property name="hibernate.cache.use_minimal_puts">true</property>
        <property name="hibernate.cache.region_prefix">MemcachedCache</property>
        <property name="hibernate.memcached.memcacheClientFactory">com.googlecode.hibernate.memcached.dangamemcached.DangaMemcacheClientFactory</property>      
        <property name="hibernate.Memcached.servers"> 10.2.200.114:11211 </property>
        <property name="hibernate.Memcached.cacheTimeSeconds">300</property>
        <property name="hibernate.Memcached.connectionFactory">KetamaConnectionFactory</property>  
        <property name="hibernate.memcached.keyStrategy">HashCodeKeyStrategy</property>
        <property name="hibernate.memcached.readBufferSize">DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE</property>
        <property name="hibernate.memcached.operationQueueLength">DefaultConnectionFactory.DEFAULT_OP_QUEUE_LEN</property>
        <property name="hibernate.memcached.operationTimeout">DefaultConnectionFactory.DEFAULT_OPERATION_TIMEOUT</property>
        <property name="hibernate.memcached.hashAlgorithm">HashAlgorithm.KETAMA_HASH</property>
        <property name="hibernate.memcached.clearSupported">false</property>

就像一样

MemcachedClient client = new MemcachedClient(new KetamaConnectionFactory(), AddrUtil.getAddresses("10.2.200.114:11211"));

如果没有,如果我必须使用 hibernate.cfg.xml 中定义的属性,那么我应该如何继续?

4

1 回答 1

2

我知道这是一个老问题,但无论如何我都会回答,因为我有完全相同的问题。

如果您使用 Spring,那么您可以将其作为 bean 连接。

这是我的持久性配置的一部分:

<property name="hibernate.cache.provider_class" value="com.googlecode.hibernate.memcached.MemcachedCacheProvider" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_structured_entries" value="true" />
<property name="hibernate.memcached.cacheTimeSeconds" value="300" />
<property name="hibernate.memcached.connectionFactory" value="BinaryConnectionFactory" />

这是相关的 Spring 配置:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.memcached.servers" value="${MEMCACHIER_SERVERS}" />
                <entry key="hibernate.memcached.username" value="${MEMCACHIER_USERNAME}" />
                <entry key="hibernate.memcached.password" value="${MEMCACHIER_PASSWORD}" />
            </map>
        </property>
    </bean>

    <bean id="plainCallbackHandler" class="net.spy.memcached.auth.PlainCallbackHandler">
        <constructor-arg index="0" value="${MEMCACHIER_USERNAME}"/>
        <constructor-arg index="1" value="${MEMCACHIER_PASSWORD}"/>
    </bean>
    <bean id="authDescriptor" class="net.spy.memcached.auth.AuthDescriptor">
        <constructor-arg index="0">
            <array><value>PLAIN</value></array>
        </constructor-arg>
        <constructor-arg index="1" ref="plainCallbackHandler"/>
    </bean>
    <bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean">
        <property name="servers" value="${MEMCACHIER_SERVERS}"/>
        <property name="protocol" value="BINARY"/>
        <property name="authDescriptor" ref="authDescriptor"/>
    </bean>

然后,您当然可以随意使用它:

@Inject
private MemcachedClient memcachedClient;

我在hibernate-memcached的 1.3 版中得到了这个。

于 2013-05-03T20:04:27.560 回答