More Related Content
More from Robbin Fan (11)
缓存技术浅谈
- 12. Query Cache 状态示例 __ Query Cache ______________________________________________ Memory usage 25.77M of 64.00M %Used: 40.26 Block Fragmnt 24.73% Hits 6.98M 3.2/s Inserts 100.87M 46.9/s Insrt:Prune 34.15:1 45.5/s Hit:Insert 0.07:1
- 15. InnoDB buffer 状态示例 __ InnoDB Buffer Pool ______________________________________ Usage 1000.00 of 1000.00 %Used: 100.00 Read hit 99.99% Pages Free 0 %Total: 0.00 Data 59.69k 93.26 % Drty: 0.02 Misc 4311 6.74 Latched 0 0.00 Reads 60.30G 28.0k/s From file 7.01M 3.3/s 0.01 Ahead Rnd 98684 0.0/s Ahead Sql 9548 0.0/s Writes 86.79M 40.3/s Flushes 14.08M 6.5/s Wait Free 0 0/s
- 19. Hibernate 对象缓存配置 @Entity @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class Forest { ... } 配置 entity 对象缓存 配置关联集合的缓存 @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="CUST_ID") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public SortedSet<Ticket> getTickets() { return tickets; } 仅仅添加 Annotation 就可以了,无须编码,即可自动享受对象缓存。 Hibernate 会拦截对象的 CRUD 操作,针对对象读取操作进行缓存,针对对象修改操作自动清理缓存
- 27. OSCache Servlet 缓存示例 <filter> <filter-name>CacheFilter</filter-name> <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class> <init-param> <param-name>time</param-name> <param-value>600</param-value> </init-param> <init-param> <param-name>scope</param-name> <param-value>session</param-value> </init-param> </filter> <filter-mapping> <filter-name>CacheFilter</filter-name> <url-pattern>/news/*</url-pattern> </filter-mapping>
- 29. OSCache 的页面局部缓存 <%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %> <cache:cache> ... some jsp content ... </cache:cache> <cache:cache key="foobar" scope="session"> ... some jsp content ... </cache:cache> <cache:cache key="<%= product.getId() %>" time="1800" refresh="<%= needRefresh %>"> ... some jsp content ... </cache:cache> <cache:cache key="<%= product.getId() %>" cron="0 2 * * *" refresh="<%= needRefresh %>"> ... some jsp content ... </cache:cache>
- 34. 未使用 AJAX 缓存代码 1. <script src="jQuery.js"></script> 2. <script language="JavaScript"> 3. <!-- 4. function gopage(option,obj){ 5. var page = $("#page").html() * 1; 6. obj.disabled = true; 7. if(option == '+'){ 8. page ++ ; 9. var url = "data.php?page="+page + "&r="+Math.random(); 10. // 获取 ajax url ,后面的 random 是为了防止浏览器缓存 xml 内容 11. }else{ 12. page --; 13. if(page < 1)page =1; 14. var url = "data.php?page="+page + "&r="+Math.random(); 15. 16. } 17. $("#data").html("loading...");// 设置 loading 状态 18. $("#page").html(page); 19. // 下面是 ajax 处理 20. $("#data").load(url, 21. {limit: 25}, 22. function() { obj.disabled = false; } 23. ); 24. 25. } 26. //--> 27. </script> 28. <input value="<" onclick="gopage('-',this)" type="button"> 29. <input value=">" onclick="gopage('+',this)" type="button"> 30. page:<span id="page">1</span> 31. <div style="border: 2px solid red; padding: 2px;" id="data">data area</div>
- 35. 使用 AJAX 缓存代码 # <script language="JavaScript"> # <!-- # var cache_data = new Array();// 定义全局变量用来保存缓存数据 # function gopageCache(option,obj){ # var page = $("#page2").html() * 1; # if(option == '+'){ # page ++ ; # var url = "data.php?page="+page + "&r="+Math.random(); # }else{ # page --; # if(page < 1)page =1; # var url = "data.php?page="+page + "&r="+Math.random(); # # } # /* 下面是缓存增加的部分 */ # if( (cache_data[page] !=null) && (cache_data[page].length > 1) ) { # // 如果缓存存在 , 则直接调用缓存数据 , 不用再去服务器进行数据请求 # alert('cache hit'); # $("#data2").html(cache_data[page]); # $("#page2").html(page); # return true; # } # $("#page2").html(page); # obj.disabled = true; # $("#data2").html("loading(cache enabled)..."); # $("#data2").load(url, # {limit: 25}, # function(responseText) { obj.disabled = false; # cache_data[page] = responseText;// 将当前的数据存入到内存 ( 缓存变量 ) 中 } # ); # # } # # //--> # </script> # <input value="<" onclick="gopageCache('-',this)" type="button"> # <input value=">" onclick="gopageCache('+',this)" type="button"> # page:<span id="page2">1</span> # <div style="border: 2px solid red; padding: 2px;" id="data2">data area</div>
- 36. 基于 HTTP 协议的资源缓存 Etag "12523074“ Last-Modified Thu, 29 May 2008 09:43:46 GMT /blog /blog If-Modified-Since Thu, 29 May 2008 09:43:46 GMT If-None-Match "12523074 " 304 Not Modified
- 37. 基于资源的缓存示例 <filter> <filter-name>CacheFilterStaticContent</filter-name> <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class> <init-param> <param-name>expires</param-name> <param-value>time</param-value> </init-param> </filter> <filter-mapping> <filter-name>CacheFilterStaticContent</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>