Any way to cache query results from google cloud endpoints?

戏子无情 提交于 2019-12-08 09:35:53

问题


I need to store the results of the query that i fired the last time locally on the client android app for immediately using the data. The updated data can be fetched later from an AsyncTack or a thread

The objects that the generated client libraries return are of class that extends GenericJson

is there any way to convert this into a String and store in sharedpreferences and Later instantiate the Objects again ?

heres the class definition

  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available

package sanket.pinboard.backend.postApi.model;

public final class Post extends com.google.api.client.json.GenericJson {
    @com.google.api.client.util.Key
    @com.google.api.client.json.JsonString
    private java.lang.Long boardID;
    @com.google.api.client.util.Key
    private java.lang.String description;
    @com.google.api.client.util.Key
    private java.lang.String filrUrl;
    @com.google.api.client.util.Key
    @com.google.api.client.json.JsonString
    private java.lang.Long id;
    @com.google.api.client.util.Key
    private java.lang.String imageUrl;
    @com.google.api.client.util.Key
    private java.lang.Boolean isOfficial;
    @com.google.api.client.util.Key
    private java.lang.Boolean isPinned;
    @com.google.api.client.util.Key
    @com.google.api.client.json.JsonString
    private java.lang.Long reminder;
    @com.google.api.client.util.Key
    @com.google.api.client.json.JsonString
    private java.lang.Long timeStamp;
    @com.google.api.client.util.Key
    private java.lang.String title;
    @com.google.api.client.util.Key
    private java.lang.String userID;

    public Post() { /* compiled code */ }

    public java.lang.Long getBoardID() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setBoardID(java.lang.Long boardID) { /* compiled code */ }

    public java.lang.String getDescription() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setDescription(java.lang.String description) { /* compiled code */ }

    public java.lang.String getFilrUrl() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setFilrUrl(java.lang.String filrUrl) { /* compiled code */ }

    public java.lang.Long getId() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setId(java.lang.Long id) { /* compiled code */ }

    public java.lang.String getImageUrl() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setImageUrl(java.lang.String imageUrl) { /* compiled code */ }

    public java.lang.Boolean getIsOfficial() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setIsOfficial(java.lang.Boolean isOfficial) { /* compiled code */ }

    public java.lang.Boolean getIsPinned() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setIsPinned(java.lang.Boolean isPinned) { /* compiled code */ }

    public java.lang.Long getReminder() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setReminder(java.lang.Long reminder) { /* compiled code */ }

    public java.lang.Long getTimeStamp() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setTimeStamp(java.lang.Long timeStamp) { /* compiled code */ }

    public java.lang.String getTitle() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setTitle(java.lang.String title) { /* compiled code */ }

    public java.lang.String getUserID() { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post setUserID(java.lang.String userID) { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post set(java.lang.String fieldName, java.lang.Object value) { /* compiled code */ }

    public sanket.pinboard.backend.postApi.model.Post clone() { /* compiled code */ }
}

回答1:


I solved the problem by implementing an amazing library

android-easy-cache

sample code using the above library :

 public class MainListingsUpdate extends AsyncTask<Void, Void, Void> {

    private CollectionResponseMainEventListing list = new CollectionResponseMainEventListing();
    private DualCache<CollectionResponseMainEventListing> cache;

     @Override
        protected Void doInBackground(Void... voids) {
            try {
                list = mainActivityConnect.getAppUserApi().getMainListings().execute();



        SizeOf<CollectionResponseMainEventListing> sizeOf = new SizeOf<CollectionResponseMainEventListing>() {
                @Override
                public int sizeOf(CollectionResponseMainEventListing object) {
                    return object.toString().getBytes().length;
                }
            };

        cache = new DualCacheBuilder<>(Constants.cacheName, 1, CollectionResponseMainEventListing.class)
                    .useReferenceInRam(Integer.MAX_VALUE, sizeOf)
                    .useDefaultSerializerInDisk(Integer.MAX_VALUE, true);

        cache.put("OBJECT_KEY", list);

            } catch (IOException e) {
                queryComplete = false;
            }
            return null;
        }

    }

and to retrieve , its as simple as :

cache.get("OBJECT_KEY");


来源:https://stackoverflow.com/questions/28255514/any-way-to-cache-query-results-from-google-cloud-endpoints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!