RepositoryCache


Source link: https://github.com/kuassivi/RepositoryCache

RepositoryCache

Annotation Processor that aims to help developers to get an easy strategy for their repositories.

This is a Java library that can be imported both in Android or Java projects.

Features:

  • Fast implementation for each repository method.
  • Provides simple methods like isCached or isExpired.
  • Distinguish between different calls of the same method.
  • Provides the ability to store content on disk without the need of a database.

Dependency

Latest stable version:

In order to generate the proxy classes when building your project, you need to apply the android-apt gradle plugin to run annotation processing.

If you are working with gradle, add the dependency to your build.gradle file:

dependencies{

  compile 'com.kuassivi.annotation:repository-cache:?.?.?'
  apt 'com.kuassivi.compiler:repository-cache-compiler:?.?.?' 
}

If you are working with maven, do it into your pom.xml

<dependency>
  <groupId>com.kuassivi.annotation</groupId>
  <artifactId>repository-cache</artifactId>
  <version>?.?.?</version>
  <type>pom</type> </dependency> <dependency>
  <groupId>com.kuassivi.compiler</groupId>
  <artifactId>repository-cache-compiler</artifactId>
  <version>?.?.?</version>
  <type>pom</type> </dependency>

How to:

Suppose you often define some contracts for your repositories. If so then you can annotate each of your methods with the RepositoryCache annotation and provide a time to expire.

Regular classes, not just interfaces, can be annotated as well.

Lets see an example:

public interface MyRepository {

@RepositoryCache(DateUtils.MINUTE_IN_MILLIS * 60) // expiration time in ms.
  Object getDataById(int id);

}

Now build your project!

Once you have built the project, a new Java Proxy Class will be created in your build source apt folder.

The Proxy Class generated will looks like: RepositoryClassName + ProxyCache suffix.

For instance, MyRepositoryProxyCache.

Lets see how to use the Proxy Class:

public class MyRepositoryImpl implements MyRepository {

@Override
  public Object getDataById(int id) {

  // Gets an instance of the cached method

RepositoryProxyCache cache = MyRepositoryProxyCache.getDataById(context.getCacheDir());

// Ensure we are getting distinct cache instance per id.

cache.select( id );
 // Optional

// Checks whether it is cached and not expired.

if (!cache.isExpired()) {

 // [...] proceed retrieving data from your local store (maybe a database)

 
}
 else {

 // [...] otherwise proceed retrieving data from your cloud store (maybe a rest service)

  // Once you persist, it will store this method name with the specified id in the cache.

 if(!cache.isCached()) {

  cache.persist();

 
}

 
}

  
}

}

Tips:

- Use always an Interface as a contract. It is much clean.

- You can cache a method indefinitely, simply do not put any time on the annotation.

- Remember you can distinguish between calls with an ID or any other logic through the select() method.

- You can i.e. store your content inside the cache with persist(String) if you are not planning to have a database. Just transform your Object from/to a Json string and retrieve that content later with getContent().

ProxyCache methods:

  • `select(Object)` - distinguish between different calls of the same method, the parameter should be any kind of an id of your choice. 
  • `isCached()` - returns true if a method call is cached, false otherwise. 
  • `isExpired()` - returns true if a method call is not cached or is expired, false otherwise. 
  • `persist()` - stores a method call in the cache. 
  • `persist(String)` - stores a method call in the cache with the provided content. 
  • `getContent()` - retrieves the stored content of a specific method call. 
  • `evict()` - removes a specific method call from the cache. 

RepositoryCacheManager utilities:

  • `static hashMD5(String)` - generates a MD5 hash string of the provided String parameter. 
  • `static hashCode(Object...)` - generates a hash code from the provided parameter objects. 
  • `static evictAll(File)` - removes all cache repositories. You must provide the cache directory. 

License

Copyright 2016 Francisco Gonzalez-Armijo Riádigos

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Resources

Power your app's data links with named entity recognition and disambiguation.

This project makes it easy to convert an installed Android app that was built with the Android SDK into readable source files.

The Android Decompile is a script that combines different tools for successfully decompiling any Android package (APK) to it's Java source-code and resources (including the AndroidManifest.xml, 9-patches, layout files, etc.).

HeiPermission is library to work with permissions from Android M.

When you publish new version android app, maybe you want all your application user update new version.

Android PanelView dashboard.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes