在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中定义的属性,那我该怎么办?
答案 0 :(得分: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版本。