javascript
简单的Spring Memcached – Spring缓存抽象和Memcached
在任何讀取繁重的數據庫應用程序中,緩存仍然是最基本的性能增強機制之一。 Spring 3.1發行版提供了一個很酷的新功能,稱為Cache Abstraction 。 Spring Cache Abstraction為應用程序開發人員提供了一種簡單,透明和分離的方式來實現任何緩存解決方案。 Memcached是跨應用程序使用的最受歡迎的分布式緩存系統之一。 在本文中,我們將重點介紹如何將Memcached與啟用Spring的應用程序集成。 由于Spring僅直接支持Ehcache和ConcurrentHashMap,因此我們將使用第三方庫Simple Spring Memcache來利用Spring緩存抽象的功能。
獲取代碼
可以從以下SVN位置下載本教程的代碼。 https://www.assembla.com/code/weblog4j/subversion/nodes/24/SpringDemos/trunk為了使本教程正常工作,請在您的數據庫中創建下表。 然后在springcache.xml中修改數據源。
CREATE TABLE IF NOT EXISTS `adconnect`.`books` (`book_id` INT NOT NULL AUTO_INCREMENT ,`book_name` VARCHAR(500) NULL ,`book_author` VARCHAR(500) NULL ,`category` VARCHAR(500) NULL ,`numpages` INT NULL ,`price` FLOAT NULL ,PRIMARY KEY (`book_id`) ) ENGINE = InnoDB;整合步驟
1. 依賴關系–我還假設您已經設置了休眠,彈簧和日志。 因此,要下載SSM依賴項,請在POM中添加以下內容。 有關全套依賴項,請從上面的SVN網址下載該項目。
<dependency><groupId>com.google.code.simple-spring-memcached</groupId><artifactId>spring-cache</artifactId><version>3.1.0</version> </dependency><dependency><groupId>com.google.code.simple-spring-memcached</groupId><artifactId>xmemcached-provider</artifactId><version>3.1.0</version> </dependency>2. 啟用緩存 –要在您的spring應用程序中啟用緩存,請在spring上下文xml中添加以下內容。
<cache:annotation-driven/>3. 配置Spring以啟用基于Memcached的緩存 –在應用程序上下文xml中添加以下內容。
<bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager"><property name="caches"><set><bean class="com.google.code.ssm.spring.SSMCache"><constructor-arg name="cache" index="0" ref="defaultCache"/><!-- 5 minutes --><constructor-arg name="expiration" index="1" value="300"/><!-- @CacheEvict(..., "allEntries" = true) doesn't work --><constructor-arg name="allowClear" index="2" value="false"/></bean></set></property></bean><bean name="defaultCache" class="com.google.code.ssm.CacheFactory"><property name="cacheName" value="defaultCache"/><property name="cacheClientFactory"><bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl"/></property><property name="addressProvider"><bean class="com.google.code.ssm.config.DefaultAddressProvider"><property name="address" value="127.0.0.1:11211"/></bean></property><property name="configuration"><bean class="com.google.code.ssm.providers.CacheConfiguration"><property name="consistentHashing" value="true"/></bean></property></bean>SSMCacheManager擴展了org.springframework.cache.support.AbstractCacheManager –它是一個抽象類,并且是基礎緩存的管理器。
SSMCache實現org.springframework.cache.Cache –這是底層緩存客戶端api的實際包裝器回合。
4. 注釋驅動緩存 – Spring使用注釋來標記要由緩存管理的方法。 這些是Spring緩存框架定義的注釋
@Cacheable演示
@Cacheable(value = "defaultCache", key = "new Integer(#book_id).toString().concat('.BookVO')")public BookVO get(int book_id) throws Exception {BookVO bookVO = null;try{Query query = getSession().createQuery("from BookVO bookVO where bookVO.book_id=:book_id");query.setLong("book_id", book_id);bookVO = (BookVO)query.uniqueResult();}catch(HibernateException he){log.error("Error in finding a bookVO : " + he);throw new Exception("Error in finding adPicVO by book_id for book_id : " + bookVO, he);}return bookVO;}請注意注釋的鍵屬性。 這是Spring Expression Language的示例。 您可以根據需要使用SePL use創建memcache密鑰。 在此示例中,我想要一個鍵,其格式應為<book_id> .BookVO。
另一個示例–假設我要存儲給定作者的bookVO列表,在這種情況下,我可以使用格式為<author_name> .BookVOList的唯一鍵,因此可以使用以下鍵
@Cacheable(value = "defaultCache", key = "#author.concat('.BookVOList')")public List<BookVO> getList(String author) throws Exception {@CachePut演示
@CachePut(value = "defaultCache", key = "new Integer(#bookVO.book_id).toString().concat('.BookVO')")public BookVO create(BookVO bookVO) throws Exception {try{getSession().save(bookVO);getSession().flush();}catch(HibernateException he){log.error("Error in inserting bookVO : " + he);throw new Exception("Error in inserting bookVO", he);}return bookVO;}插入數據時可以使用CachePut,插入后可以將插入的數據放入緩存中
@CacheEvict演示
@CacheEvict(value = "defaultCache", key = "new Integer(#bookVO.book_id).toString().concat('.BookVO')")public BookVO update(BookVO bookVO) throws Exception {try{Query query = getSession().createQuery("update BookVO bookVO set bookVO.book_name=:book_name, bookVO.book_author=:book_author,bookVO.category=:category,bookVO.numpages=:numpages,bookVO.price=:price " +"where bookVO.book_id=:book_id");query.setString("book_name", bookVO.getBook_name());query.setString("book_author", bookVO.getBook_author());query.setString("category", bookVO.getCategory());query.setInteger("numpages", bookVO.getNumpages());query.setFloat("price", bookVO.getPrice());query.setLong("book_id", bookVO.getBook_id());query.executeUpdate();}catch(HibernateException he){log.error("Error in updating bookVO : " + he);throw new Exception("Error in updating bookVO", he);}return bookVO;}資源資源
翻譯自: https://www.javacodegeeks.com/2013/06/simple-spring-memcached-spring-caching-abstraction-and-memcached.html
總結
以上是生活随笔為你收集整理的简单的Spring Memcached – Spring缓存抽象和Memcached的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创维安卓系统升级(创维安卓系统)
- 下一篇: MOXy是GlassFish 4中新的默