Java Web Application: How to implement caching techniques?

前端 未结 6 2036
别那么骄傲
别那么骄傲 2020-12-12 18:16

I am developing a Java web application that bases it behavior through large XML configuration files that are loaded from a web service. As these files are not actually requi

6条回答
  •  伪装坚强ぢ
    2020-12-12 19:07

    Bref , you can use this ready spring ehcache configuration

    1- ehcache.xml : show global configuration of Ehcache.

    
    
        
        
    
    
    

    2- ehcache-spring.xml : create EhCacheManagerFactoryBean and EhCacheFactoryBean.

        
            
            
    
        
    
     
            
            
            
            
            
            
            
            
            
            
            
        
    

    3- Inject "myCache" bean in your business class , see the following exemple to get started with getting and putting a object in your cache.

    @Resource("myCache")
    private net.sf.ehcache.Cache myCache; 
    
    @Resource("myService")
    private Service myService; 
    
    public byte[] getFromCache(final String code) 
    { 
    // init Cache 
    final StringBuilder builder = new StringBuilder(); 
     // key to identify a entry in cache map
    final String key = code;
    // get form the cache 
    final Element element = myCache.get(key); 
    if (element != null && element.getValue() != null) 
    { 
           return (byte[]) element.getValue(); 
    } 
    
    final byte[] somethingToBeCached = myService.getBy(code); 
    // store in the cache
    myCache.put(new Element(key, somethingToBeCached)); 
    
    return somethingTobeCached; 
    
    } 
    

提交回复
热议问题