Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to build custom keys for disk caching #178

Closed
bubbleguuum opened this issue Oct 7, 2014 · 0 comments · Fixed by #179
Closed

Easier way to build custom keys for disk caching #178

bubbleguuum opened this issue Oct 7, 2014 · 0 comments · Fixed by #179
Assignees
Milestone

Comments

@bubbleguuum
Copy link

When an image is cached to disk, it is pinned to its URL (and other attributes, but let's only consider the URL).
If the image pointed by the URL changes, the old cached entry is used, resulting in the wrong image displayed.

In my use case, I have additional data (a String but could be something else) associated to the URL that changes if the image pointed by the URL has changed.
So pinning cached images to URL+data instead of just URL, solves nicely the problem.

However, Glide doesn't make this easy and requires quite a bit of boiler plate code, as key building is buried inside DataFetcher.getId():

public static class CustomGlideUrl extends GlideUrl {

        String data;

        public CustomGlideUrl(String url, String data) {
            super(url);
            this.data = data;
        }

        public String getData() {
            return data;
        }
    }

static public class CustomOkHttpUrlLoader implements ModelLoader<CustomGlideUrl, InputStream> {

        /**
         * The default factory for {@link OkHttpUrlLoader}s.
         */
        public static class Factory implements ModelLoaderFactory<CustomGlideUrl, InputStream> {
            private static volatile OkHttpClient internalClient;
            protected OkHttpClient client;
            protected String userAgent;

            private static OkHttpClient getInternalClient() {
                if (internalClient == null) {
                    synchronized (Factory.class) {
                        if (internalClient == null) {
                            internalClient = new OkHttpClient();
                        }
                    }
                }
                return internalClient;
            }

            /**
             * Constructor for a new Factory that runs requests using a static singleton client.
             */
            public Factory(String userAgent) {
                this(getInternalClient(), userAgent);
            }

            /**
             * Constructor for a new Factory that runs requests using given client.
             */
            public Factory(OkHttpClient client, String userAgent) {
                this.client = client;
                this.userAgent = userAgent;
            }

            @Override
            public ModelLoader<CustomGlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
                return new CustomOkHttpUrlLoader(client, userAgent);
            }

            @Override
            public void teardown() {
                // Do nothing, this instance doesn't own the client.
            }
        }

        protected final OkHttpClient client;
        protected final String userAgent;

        public CustomOkHttpUrlLoader(OkHttpClient client, String userAgent) {
            this.client = client;
            this.userAgent = userAgent;
        }

        @Override
        public DataFetcher<InputStream> getResourceFetcher(final CustomGlideUrl model, int width, int height) {
             return new OkHttpStreamFetcher(client, model, userAgent) {
                 @Override
                    public String getId() {
                        return super.getId() + model.getData();
                    }
            };
        }
    }
           Glide.get(this).register(CustomGlideUrl.class, InputStream.class, new CustomOkHttpUrlLoader.Factory(null));


          Glide.with(this).load(new CustomGlideUrl(url, "some data that changes if image pointed by url changes")...;

It could be interesting to make custom key building easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants