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

amplify.request.cache._key is sometimes doesn't resulting unique value #77

Open
budiadiono opened this issue Jan 19, 2013 · 3 comments
Open

Comments

@budiadiono
Copy link

To reproduce this issue please try this at browser console:

amplify.request.cache._key('ajax1', '/api/opqrstu/opqrstu-project-1/project/get')

it will resulting "request-ajax1-992640007"

now try with this:

amplify.request.cache._key('ajax1', '/api/abcdefg/abcdefg-project-1/project/get')

then you'll get same result "request-ajax1-992640007"

@drakeh
Copy link

drakeh commented Feb 15, 2013

Furthermore,

amplify.request.cache._key('foo', 'foo?a=1&b=2') === amplify.request.cache._key('foo', 'foo?a=2&b=1')

evaluates to true

@toarnold
Copy link

On more: 'x' and 'xxxxxxxxx' gets the same hashvalue, because 'xxxx' XOR 'xxxx' evaluates to zero! Furthermore it seems the algorithm doesn't takes care of unicode - all values shifted by 8 bits.

My suggestions is to use/override the _key function with a better one (e.g. java hashCode or crc32):

        amplify.request.cache._key = function (resourceId, url, data) {

            function hashCode(s) {
                var hash = 0, i, chr, len = s.length;
                if (len === 0) {
                    return hash;
                }
                for (i = 0; i < len; i++) {
                    chr = s.charCodeAt(i);
                    hash = ((hash << 5) - hash) + chr;
                    hash |= 0;
                }
                return hash;
            };

            return 'request-' + resourceId + '-' + hashCode(url + data).toString(16);
        };

@quidquid
Copy link

quidquid commented Jan 9, 2015

Yah, seriously this hash needs to be changed. Within a couple of hours of trying out amplify I ran into puzzling, buggy behavior that indeed turned out to be cache key collisions.

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

No branches or pull requests

4 participants