@Override public Object desanitizeKey(Object sanitzedCacheKey) throws KeySanitationExcepion { if (!(sanitzedCacheKey instanceof String)) { throw new KeySanitationExcepion(DefaultKeySanitizer.class.getSimpleName() + " can only be used with Strings cache keys."); } try { return new String(Base64.decode((String) sanitzedCacheKey, BASE64_FLAGS), UTF8_CHARSET_NAME); } catch (UnsupportedEncodingException e) { throw new KeySanitationExcepion(e); } } }
@Override public long getCreationDateInCache(Object cacheKey) throws CacheLoadingException { File cacheFile = getCacheFile(cacheKey); if (cacheFile.exists()) { return cacheFile.lastModified(); } else { throw new CacheLoadingException( "Data could not be found in cache for cacheKey=" + cacheKey); } }
@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { String resultJson; // transform the content in json to store it in the cache resultJson = mJsonMapper.writeValueAsString(data); // finally store the json in the cache if (!StringUtils.isEmpty(resultJson)) { FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson, CharEncoding.UTF_8); } else { throw new CacheSavingException("Data was null and could not be serialized in json"); } }
/** * Sets the folder used by object persisters of this factory. * @param cacheFolder * the new cache folder of this factory (and persisters it will * create). Ca be null, it will then default to the sub folder * {@link InFileObjectPersister#DEFAULT_ROOT_CACHE_DIR} in side * the application cache folder. Will be created if doesn't exist * yet. * @throws CacheCreationException */ public void setCacheFolder(File cacheFolder) throws CacheCreationException { if (cacheFolder == null) { cacheFolder = new File(getApplication().getCacheDir(), InFileObjectPersister.DEFAULT_ROOT_CACHE_DIR); } this.cacheFolder = cacheFolder; if (!cacheFolder.exists() && !cacheFolder.mkdirs()) { throw new CacheCreationException("The cache folder " + cacheFolder.getAbsolutePath() + " could not be created."); } }
@Override protected T deserializeData(String json) throws CacheLoadingException { try { return mJsonMapper.readValue(json, getHandledClass()); } catch (Exception e) { throw new CacheLoadingException(e); } }
@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { String resultJson; // transform the content in json to store it in the cache resultJson = gson.toJson(data); // finally store the json in the cache if (!StringUtils.isEmpty(resultJson)) { FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson, CharEncoding.UTF_8); } else { throw new CacheSavingException("Data was null and could not be serialized in json"); } }
/** * Set the cacheFolder to use. * @param cacheFolder * the new cache folder to use. Can be null, will then default to * {@link #DEFAULT_ROOT_CACHE_DIR} sub folder in the application * cache dir. * @throws CacheCreationException * if the cache folder doesn't exist or can't be created. */ public void setCacheFolder(File cacheFolder) throws CacheCreationException { if (cacheFolder == null) { cacheFolder = new File(getApplication().getCacheDir(), DEFAULT_ROOT_CACHE_DIR); } synchronized (cacheFolder.getAbsolutePath().intern()) { if (!cacheFolder.exists() && !cacheFolder.mkdirs()) { throw new CacheCreationException("The cache folder " + cacheFolder.getAbsolutePath() + " could not be created."); } } this.cacheFolder = cacheFolder; }
@Override public Object sanitizeKey(Object cacheKey) throws KeySanitationExcepion { if (!(cacheKey instanceof String)) { throw new KeySanitationExcepion(DefaultKeySanitizer.class.getSimpleName() + " can only be used with Strings cache keys."); } try { return Base64.encodeToString(((String) cacheKey).getBytes(UTF8_CHARSET_NAME), BASE64_FLAGS); } catch (UnsupportedEncodingException e) { throw new KeySanitationExcepion(e); } }
@Override protected T deserializeData(String json) throws CacheLoadingException { try { return mJsonMapper.readValue(json, getHandledClass()); } catch (Exception e) { throw new CacheLoadingException(e); } }
@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { try { serializer.write(data, getCacheFile(cacheKey)); } catch (Exception e) { throw new CacheSavingException("Data was null and could not be serialized in xml"); } } }
@Override public long getCreationDateInCache(Object cacheKey) throws CacheLoadingException { CacheEntry cacheEntry = null; try { cacheEntry = databaseHelper.queryCacheKeyForIdFromDatabase(String.valueOf(cacheKey)); } catch (SQLException e) { throw new CacheLoadingException("Data could not be found in cache for cacheKey=" + cacheKey, e); } if (cacheEntry == null) { throw new CacheLoadingException("Data could not be found in cache for cacheKey=" + cacheKey); } return cacheEntry.getTimestamp(); }
@Override public InputStream saveDataToCacheAndReturnData(InputStream data, Object cacheKey) throws CacheSavingException { FileOutputStream output = null; // special case for big inputstream object : as it can be read // only once and is too big to be locally // duplicated, // 1) we save it in file // 2) we load and return it from the file try { output = new FileOutputStream(getCacheFile(cacheKey)); IOUtils.copy(data, output); return new FileInputStream(getCacheFile(cacheKey)); } catch (IOException e) { throw new CacheSavingException(e); } finally { IOUtils.closeQuietly(output); } }
@Override protected String readCacheDataFromFile(File file) throws CacheLoadingException { try { return FileUtils.readFileToString(file, CharEncoding.UTF_8); } catch (FileNotFoundException e) { // Should not occur (we test before if // file exists) // Do not throw, file is not cached Ln.w("file " + file.getAbsolutePath() + " does not exists", e); return null; } catch (Exception e) { throw new CacheLoadingException(e); } }
@Override public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException { try { if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { saveData(data, cacheKey); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } catch (CacheSavingException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { saveData(data, cacheKey); } } catch (CacheSavingException e) { throw e; } catch (Exception e) { throw new CacheSavingException(e); } return data; }
@Override protected T deserializeData(String xml) throws CacheLoadingException { try { return serializer.read(getHandledClass(), xml); } catch (Exception e) { throw new CacheLoadingException(e); } }
@Override public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException { try { if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { saveData(data, cacheKey); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } catch (CacheSavingException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { saveData(data, cacheKey); } } catch (CacheSavingException e) { throw e; } catch (Exception e) { throw new CacheSavingException(e); } return data; }
@Override public long getCreationDateInCache(Object cacheKey) throws CacheLoadingException { CacheItem<T> cacheItem = lruCache.get(cacheKey); if (cacheItem != null) { return cacheItem.getCreationDate(); } else { if (decoratedPersister != null) { return decoratedPersister.getCreationDateInCache(cacheKey); } } throw new CacheLoadingException("Data could not be found in cache for cacheKey=" + cacheKey); }
@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { String resultJson; // transform the content in json to store it in the cache resultJson = mJsonMapper.writeValueAsString(data); synchronized (getCacheFile(cacheKey).getAbsolutePath().intern()) { // finally store the json in the cache if (!StringUtils.isEmpty(resultJson)) { FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson, CharEncoding.UTF_8); } else { throw new CacheSavingException("Data was null and could not be serialized in json"); } } }
@Override protected T readCacheDataFromFile(File file) throws CacheLoadingException { try { JsonParser jsonParser = jsonFactory.createJsonParser(new FileReader(file)); T result = jsonParser.parse(getHandledClass(), null); jsonParser.close(); return result; } catch (FileNotFoundException e) { // Should not occur (we test before if file exists) // Do not throw, file is not cached Ln.w("file " + file.getAbsolutePath() + " does not exists", e); return null; } catch (Exception e) { throw new CacheLoadingException(e); } }
@Override public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException { try { if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { saveData(data, cacheKey); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } catch (CacheSavingException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { saveData(data, cacheKey); } } catch (CacheSavingException e) { throw e; } catch (Exception e) { throw new CacheSavingException(e); } return data; }