Skip to main content
deleted 55 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I want to create a singleton object of a third party caching library (ThirdPartyKVStore). But I don't want to reference this class all over the placesplace from where I want to access the cache. To address this, I created a wrapper CacheManager which is managing the lifecycle of ThirdPartyKVStore. Does it look ok?

PS: I'm using StackExchange for the first time.

I want to create singleton object of a third party caching library (ThirdPartyKVStore). But I don't want to reference this class all over the places from where I want to access the cache. To address this, I created a wrapper CacheManager which is managing the lifecycle of ThirdPartyKVStore. Does it look ok?

PS: I'm using StackExchange for the first time.

I want to create a singleton object of a third party caching library (ThirdPartyKVStore). But I don't want to reference this class all over the place from where I want to access the cache. To address this, I created a wrapper CacheManager which is managing the lifecycle of ThirdPartyKVStore. Does it look ok?

added 244 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Create another SingeltonSingleton object from a SingeltonSingleton class

public class CacheManager {

     private static ThirdPartyKVStore thirdPartyCache = null;
     private static volatile CacheManager cacheManager = null;

     private CacheManager(){

     }

     public static CacheManager getConnection(CacheConfig cacheConfig) {
       if (cacheManager == null) {
         synchronized (CacheManager.class) {
           if (cacheManager == null) {
            try {
              thirdPartyCache = new ThirdPartyKVStore(cacheConfig);            
              cacheManager = new CacheManager();
           } catch (Throwable e) {
            logger.error("Fn=getConnection Msg=Cache_Initialization_Error_Occurred.", e);
          }
        }
      }
     }
    return cacheManager;
    }

    public static void closeCacheConnection(){
      if(thirdPartyCache != null){
          thirdPartyCache.shutdown();
        }
    }

    public <T> T getCacheData(String key){
      T cachedObject = null;
      String cachedStr = null;

      Entry<String> e = thirdPartyCache.get(key);
      if(e != null){
        cachedStr = e.getPayload();
       }
      if (cachedStr != null) {
             TypeToken<T> typeToken = new TypeToken<T>() {};
             cachedObject = deserializeStr2POJO(cachedStr, typeToken);
     }
    return cachedObject;
    }


    public <T> void setCacheData(String key, T t){
        if (thirdPartyCache != null) {
              try {
                  Entry<String> e = new Entry.Builder<String>(new Gson()
                          .toJson(t))
                          .withVersion(DEFAULT_VERSION)
                          .withApplication(DEFAULT_APP)
                          .build();
            
                  thirdPartyCache.set(key, e, Integer.MAX_VALUE);        
                } catch (Exception e) {
              }
          }
     }
}

}

Create another Singelton object from a Singelton class

public class CacheManager {

  private static ThirdPartyKVStore thirdPartyCache = null;
  private static volatile CacheManager cacheManager = null;

 private CacheManager(){

 }

 public static CacheManager getConnection(CacheConfig cacheConfig) {
   if (cacheManager == null) {
     synchronized (CacheManager.class) {
       if (cacheManager == null) {
        try {
          thirdPartyCache = new ThirdPartyKVStore(cacheConfig);            
          cacheManager = new CacheManager();
       } catch (Throwable e) {
        logger.error("Fn=getConnection Msg=Cache_Initialization_Error_Occurred.", e);
      }
    }
  }
 }
return cacheManager;
}

public static void closeCacheConnection(){
  if(thirdPartyCache != null){
      thirdPartyCache.shutdown();
    }
}

public <T> T getCacheData(String key){
  T cachedObject = null;
  String cachedStr = null;

  Entry<String> e = thirdPartyCache.get(key);
  if(e != null){
    cachedStr = e.getPayload();
   }
  if (cachedStr != null) {
         TypeToken<T> typeToken = new TypeToken<T>() {};
         cachedObject = deserializeStr2POJO(cachedStr, typeToken);
 }
return cachedObject;
}


public <T> void setCacheData(String key, T t){
    if (thirdPartyCache != null) {
          try {
              Entry<String> e = new Entry.Builder<String>(new Gson()
                      .toJson(t))
                      .withVersion(DEFAULT_VERSION)
                      .withApplication(DEFAULT_APP)
                      .build();
        
              thirdPartyCache.set(key, e, Integer.MAX_VALUE);        
            } catch (Exception e) {
          }
      }
 }

}

Create another Singleton object from a Singleton class

public class CacheManager {

     private static ThirdPartyKVStore thirdPartyCache = null;
     private static volatile CacheManager cacheManager = null;

     private CacheManager(){

     }

     public static CacheManager getConnection(CacheConfig cacheConfig) {
       if (cacheManager == null) {
         synchronized (CacheManager.class) {
           if (cacheManager == null) {
            try {
              thirdPartyCache = new ThirdPartyKVStore(cacheConfig);            
              cacheManager = new CacheManager();
           } catch (Throwable e) {
            logger.error("Fn=getConnection Msg=Cache_Initialization_Error_Occurred.", e);
          }
        }
      }
     }
    return cacheManager;
    }

    public static void closeCacheConnection(){
      if(thirdPartyCache != null){
          thirdPartyCache.shutdown();
        }
    }

    public <T> T getCacheData(String key){
      T cachedObject = null;
      String cachedStr = null;

      Entry<String> e = thirdPartyCache.get(key);
      if(e != null){
        cachedStr = e.getPayload();
       }
      if (cachedStr != null) {
             TypeToken<T> typeToken = new TypeToken<T>() {};
             cachedObject = deserializeStr2POJO(cachedStr, typeToken);
     }
    return cachedObject;
    }


    public <T> void setCacheData(String key, T t){
        if (thirdPartyCache != null) {
              try {
                  Entry<String> e = new Entry.Builder<String>(new Gson()
                          .toJson(t))
                          .withVersion(DEFAULT_VERSION)
                          .withApplication(DEFAULT_APP)
                          .build();
            
                  thirdPartyCache.set(key, e, Integer.MAX_VALUE);        
                } catch (Exception e) {
              }
          }
     }
}
Source Link
Pankaj
  • 101
  • 1

Create another Singelton object from a Singelton class

I want to create singleton object of a third party caching library (ThirdPartyKVStore). But I don't want to reference this class all over the places from where I want to access the cache. To address this, I created a wrapper CacheManager which is managing the lifecycle of ThirdPartyKVStore. Does it look ok?

PS: I'm using StackExchange for the first time.

public class CacheManager {

  private static ThirdPartyKVStore thirdPartyCache = null;
  private static volatile CacheManager cacheManager = null;

 private CacheManager(){

 }

 public static CacheManager getConnection(CacheConfig cacheConfig) {
   if (cacheManager == null) {
     synchronized (CacheManager.class) {
       if (cacheManager == null) {
        try {
          thirdPartyCache = new ThirdPartyKVStore(cacheConfig);            
          cacheManager = new CacheManager();
       } catch (Throwable e) {
        logger.error("Fn=getConnection Msg=Cache_Initialization_Error_Occurred.", e);
      }
    }
  }
 }
return cacheManager;
}

public static void closeCacheConnection(){
  if(thirdPartyCache != null){
      thirdPartyCache.shutdown();
    }
}

public <T> T getCacheData(String key){
  T cachedObject = null;
  String cachedStr = null;

  Entry<String> e = thirdPartyCache.get(key);
  if(e != null){
    cachedStr = e.getPayload();
   }
  if (cachedStr != null) {
         TypeToken<T> typeToken = new TypeToken<T>() {};
         cachedObject = deserializeStr2POJO(cachedStr, typeToken);
 }
return cachedObject;
}


public <T> void setCacheData(String key, T t){
    if (thirdPartyCache != null) {
          try {
              Entry<String> e = new Entry.Builder<String>(new Gson()
                      .toJson(t))
                      .withVersion(DEFAULT_VERSION)
                      .withApplication(DEFAULT_APP)
                      .build();
        
              thirdPartyCache.set(key, e, Integer.MAX_VALUE);        
            } catch (Exception e) {
          }
      }
 }

}