番石榴没有返回缓存的对象

时间:2016-02-16 17:10:29

标签: memcached guava

我正在使用Guava缓存但由于某种原因我的CacheBuilder没有返回已经缓存的对象,它总是用于昂贵的调用。我使用如下:

修改

LoadingCache<ProfileDetails, OutAuthProfile> profileCache;
private void init(int maxCacheSize) {
        // Check if we have it in our cache
        profileCache = CacheBuilder.newBuilder().maximumSize(maxCacheSize) // maximum
                                                                       // 100
                                                                       // records
                                                                       // can
                                                                       // be
                                                                       // cached
            .build(new CacheLoader<ProfileDetails, OutAuthProfile>() { // build
                                                                      // the
                                                                      // cacheloader
                @Override
                public OutAuthProfile load(ProfileDetails profileDetails) throws Exception {
                    // make the expensive call
                    return getFilterAndSelectFromT24(profileDetails);
                }
            });
    }

    /**
 * Get the AuthProfile for a provided InAuthProfile. This method will use cache, if not found in cache
 * it will call T24 to retrieve the information and store for the next run
 * @param profileDetails
 * @return
 */
public OutAuthProfile getAuthProfileForSearchDb(ProfileDetails profileDetails) {
    try {
        return profileCache.get(profileDetails);
    } catch (ExecutionException ee) {
        ResponseDetails responseDetails = new ResponseDetails();
        responseDetails.addError(new Response("EB.SMS-SERVICE-ERROR", CFConstants.RESPONSE_TYPE_FATAL_ERROR, ee
                .getMessage(), null));
        return new OutAuthProfile(null, null, responseDetails);
    }
}

我称之为:

ProfileDetails profileDetails = T24AuthHelper.getProfile(userName, companyName, t24EntityName);
OutAuthProfile outProfile = authHelper.getAuthProfileForSearchDb(profileDetails);

以下是我在ProfileDetails对象中用于equals()和hasCode()的基本实现;

public static ProfileDetails getProfile(String userName, String companyName, String t24EntityName) {
    return new ProfileDetails(userName, companyName, t24EntityName) {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof ProfileDetails)) {
                return false;
            }
            ProfileDetails other = (ProfileDetails) obj;
                return this.getProfileName() == other.getProfileName() &&
                       this.getCompany() == other.getCompany() && 
                       this.getResourceName() == other.getResourceName();
        }

        @Override
        public int hashCode() {
            int hashCode = 0;
            if (getProfileName()!= null)
                hashCode += getProfileName().hashCode();
            if (getCompany() != null) 
                hashCode += getCompany().hashCode();
            if (getResourceName() != null)
                hashCode += getResourceName().hashCode();
            return hashCode;
        }

    };
}

来自guava-testlib的测试;

@Test
public void testProfileEqualsImpl() {
    ProfileDetails profile11 = T24AuthHelper.getProfile("Name1", "Company1", "Entity1");
    ProfileDetails profile12 = T24AuthHelper.getProfile("Name1", "Company1", "Entity1");
    ProfileDetails profile21 = T24AuthHelper.getProfile("Name2", "Company2", "Entity2");
    ProfileDetails profile22 = T24AuthHelper.getProfile("Name2", "Company2", "Entity2");

    EqualsTester eqTester = new EqualsTester()
        .addEqualityGroup(profile11, profile12)
        .addEqualityGroup(profile21, profile22);
    eqTester.testEquals();
}

0 个答案:

没有答案