2013-03-18 43 views
2

在我的应用我使用cache.in该应用程序我删除了Ehcache的Ehcache第二级之后添加一些数据来db.here我把我的ehcache.xml中获得缓存名编程

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" 
monitoring="autodetect" dynamicConfig="true"> 
<diskStore path="java.io.tmpdir"/> 
<defaultCache 
     maxElementsInMemory="100000" 
     eternal="false" 
     timeToIdleSeconds="10" 
     timeToLiveSeconds="10" 
     overflowToDisk="false" 
     memoryStoreEvictionPolicy="LFU" 
     /> 
<cache 
name="com.model.Customer" 
maxElementsInMemory="100000" 
eternal="false" 
timeToIdleSeconds="10" 
timeToLiveSeconds="10" 
overflowToDisk="false" 
memoryStoreEvictionPolicy="LFU" 
/> 
<cache 
name="com.model.Friend" 
maxElementsInMemory="100000" 
eternal="false" 
timeToIdleSeconds="10" 
timeToLiveSeconds="10" 
overflowToDisk="false" 
memoryStoreEvictionPolicy="LFU" 
/> 
</ehcache> 

现在我想删除cahche使用此代码

CacheManager manager = CacheManager.getInstance(); 
Cache cache = manager.getCache(); 
cache.removeAll(); 

,所以我需要缓存name.so将如何得到我的缓存的名字吗?请帮我任何一个 是缓存的配置是否正确。这是我对插入Java代码的数据

Transaction trns = null; 
Session session = HibernateUtil.getFirstFactory().openSession(); 
try 
{ 
Customer cus=new Customer(); 
cus.setName(name); 
cus.setMobile(Long.parseLong(mno)); 
trns = session.beginTransaction(); 
Query query= session.createQuery("from Customer where name=? or mobile=?"); 
query.setParameter(0, cus.getName()); 
query.setParameter(1, cus.getMobile()); 
cus=(Customer)query.uniqueResult(); 
if(cus==null) 
{ 
    cus=new Customer(name,Long.parseLong(mno),f); 
    session.save(cus); 
    session.getTransaction().commit(); 
} 
} 
catch (Exception e) 
{ 
if(trns != null){ 
trns.rollback(); 
} 
e.printStackTrace(); 
} finally{ 
    CacheManager manager = CacheManager.getInstance(); 
    String[] names = manager.getCacheNames(); 
    System.out.println("length="+names.length);//here the output is length=0 
    for (int i=0;i<names.length;i++)//so the control terminate the loop so the cache does not remove anything 
    { 
System.out.println("name="+names[i]); 
    Cache cache = manager.getCache(names[i]); 
    cache.removeAll(); 
    } 
    session.flush(); 
    session.close(); 
} 

这是我的客户类

private Long cid; 
private String name; 
private Long mobile; 
private Set<Friend> friends = new HashSet<Friend>(0); 



public Customer(String name, Long mobile,Set<Friend> friends) { 
    this.name = name; 
    this.mobile = mobile; 
    this.friends=friends; 
} 

public Customer() {} 

public Long getCid() { 
    return cid; 
} 
public void setCid(Long cid) { 
    this.cid = cid; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 


public Long getMobile() { 
    return mobile; 
} 

public void setMobile(Long mobile) { 
    this.mobile = mobile; 
} 

public Set<Friend> getFriends() { 
    return friends; 
} 

public void setFriends(Set<Friend> friends) { 
    this.friends = friends; 
} 

,这是我Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="com.model.Customer" table="customer"> 
<cache usage="read-write"/> 

    <id column="cid" name="cid" type="java.lang.Long" > 
     <generator class="native"/> 
    </id> 
     <property name="name" /> 
     <property name="mobile" index="msisdn"/> 

    <!-- <set name="cards" table="t_customer_card" cascade="all"> 
     <key column="code" not-null="true"/> 
     <many-to-many class="com.asta.model.Card" column="cid" unique="true" /> 
    </set> --> 
     <set name="friends" table="customer_friends" cascade="all" lazy="false"> 
     <key column="cid" not-null="true"/> 
     <many-to-many class="com.model.Friend" column="fid" unique="true" /> 
    </set>  
</class> 

</hibernate-mapping> 

我不知道我错在哪里?

回答

0

缓存名称应该与您在xml文件中声明的完全相同,例如:com.model.Customer

您可以随时致电CacheManager.getCacheNames()以获取完整列表。

+0

感谢您的回复。 CacheManager manager = CacheManager.getInstance(); String [] names = manager.getCacheNames();我使用这段代码,但输出是长度= 0.因此,它没有得到任何缓存名称 – 2013-03-19 10:59:37

+0

你是否设法最终破解它(作为你接受了我的回答)?或者您的评论仍然适用? – mindas 2013-03-19 12:45:12

+0

请寄给我如何获得缓存名称的完整代码,我使用上面的代码,但它不显示任何缓存名称。 – 2013-03-20 07:09:17