1   /*
2    * (C) Copyright 1989-2012 Trend Micro, Inc.
3    * All Rights Reserved.
4    *
5    * This program is an unpublished copyrighted work which is proprietary
6    * to Trend Micro, Inc. and contains confidential information that is not
7    * to be reproduced or disclosed to any other person or entity without
8    * prior written consent from Trend Micro, Inc. in each and every instance.
9    *
10   * WARNING:  Unauthorized reproduction of this program as well as
11   * unauthorized preparation of derivative works based upon the
12   * program or distribution of copies by sale, rental, lease or
13   * lending are violations of federal copyright laws and state trade
14   * secret laws, punishable by civil and criminal penalties.
15   */
16  
17  package com.trendmicro.grid.acl.ds.cache;
18  
19  import net.sf.tinyjee.cache.CacheContext;
20  import org.infinispan.manager.EmbeddedCacheManager;
21  import org.springframework.jmx.export.annotation.ManagedAttribute;
22  import org.springframework.jmx.export.annotation.ManagedResource;
23  import org.springframework.stereotype.Component;
24  
25  import java.util.concurrent.atomic.AtomicLong;
26  
27  /**
28   * Is a shared
29   *
30   * @author Juergen_Kellerer, 2012-07-16
31   */
32  @Component
33  @ManagedResource(objectName = ThirdLevelCacheStatisticsSummary.OBJECT_NAME, description = "" +
34  		"Implements a summary on the 3rd level cache (memcached) efficiency.")
35  public class ThirdLevelCacheStatisticsSummary {
36  
37  	static final String JMX_NAME = "ThirdLevelCacheStatisticsSummary";
38  	static final String JMX_DOMAIN = "com.trendmicro.grid.acl:";
39  	static final String OBJECT_NAME = JMX_DOMAIN + "type=" + JMX_NAME + ",name=Context";
40  
41  	protected final AtomicLong fetchedKeyCount = new AtomicLong();
42  
43  	/**
44  	 * Returns the number of times that the 3rd level cache was bypassed.
45  	 *
46  	 * @return the number of times that the 3rd level cache was bypassed.
47  	 */
48  	public AtomicLong getFetchedKeyCount() {
49  		return fetchedKeyCount;
50  	}
51  
52  	/**
53  	 * Returns the number of times that the 3rd level cache was bypassed.
54  	 * <p/>
55  	 * Bypassed means that the the request hits 2nd, 1st level cache in hibernate and finally the database.
56  	 *
57  	 * @return the number of times that the 3rd level cache was bypassed.
58  	 */
59  	@ManagedAttribute(description = "The number of times that the 3rd level cache was bypassed.")
60  	public long getCacheMisses() {
61  		return fetchedKeyCount.get();
62  	}
63  
64  	/**
65  	 * The number of times that the 3rd level cache provided a normal result.
66  	 *
67  	 * @return The number of times that the 3rd level cache provided a normal result.
68  	 */
69  	@ManagedAttribute(description = "The number of times that the 3rd level cache provided a normal result.")
70  	public long getCacheHits() {
71  		return collectHits("3rdLevel.Cache");
72  	}
73  
74  	/**
75  	 * The number of times that the 3rd level cache provided a result a cached 'null' result.
76  	 *
77  	 * @return The number of times that the 3rd level cache provided a result a cached 'null' result.
78  	 */
79  	@ManagedAttribute(description = "The number of times that the 3rd level cache provided a result a cached 'null' result.")
80  	public long getNegativeCacheHits() {
81  		return collectHits("Internal.3rdLevel.Cache.Unknown");
82  	}
83  
84  	private static long collectHits(String prefix) {
85  		long hits = 0;
86  		final CacheContext context = CacheContext.getInstance();
87  		for (EmbeddedCacheManager cacheManager : context.getCacheManagers().values()) {
88  			for (String name : cacheManager.getCacheNames()) {
89  				if (name.startsWith(prefix))
90  					hits += cacheManager.getCache(name).getAdvancedCache().getStats().getHits();
91  			}
92  		}
93  		return hits;
94  	}
95  }