HttpCache design of volley frame analysis (IV.)

Source: Internet
Author: User

Remember an expert said that success lies in the details. Similarly, the quality of a piece of code also lies in the handling of the details. The more you consider, the less likely the problem will be.
The cache was written before, but after looking at volley's cache, I really felt the gap was big. No more nonsense, let's go to dinner.

 Public Static  class Entry {        /** The data returned from cache. * /         Public byte[] data;/** ETag for cache coherency. * /         PublicString ETag;/** Date of this response as reported by the server. * /         Public LongServerdate;/** the last modified date for the requested object. * /         Public LongLastModified;/** TTL for this record. * /         Public Longttl/** Soft TTL for this record. * /         Public LongSoftttl;/** Immutable response headers as received from server; must be non-null. * /         Publicmap<string, string> responseheaders = Collections.emptymap ();/** True If the entry is expired. * /         Public Boolean isexpired() {return  This. TTL < System.currenttimemillis (); }/** True If a refresh is needed from the original data source. * /         Public Boolean refreshneeded() {return  This. Softttl < System.currenttimemillis (); }    }

There is also the corresponding httpheaderparser of this class. This kind of code is longer, it is not posted here, the main

Let's introduce it to each other.
Data[] Nothing to say, is the data itself

ETAG
The etag is a definition of the HTTP header, a Web cache authentication mechanism in several mechanisms provided by the HTTP protocol, and allows the client to negotiate cache. This makes caching more efficient and bandwidth-saving. If the content of the resource does not change, the Web server does not need to send a full response. The etag can also be used for concurrency control as a way to prevent resources from being updated and overwritten by each other. The etag algorithm is not explicitly defined, as long as the probability of a conflict must not good, it can be a timestamp, can be a low-conflicting hash, or even a version number.
How to use the ETag? Typically added by the server in the returned Httpheader

ETag"686897696a7c876b7e"

The client then requests the same URL again with the

If-None-Match"686897696a7c876b7e"

If the server discovers that the resource is not changed, it returns 304 directly, the client can use the local cache directly, and if the server resource is changed, the full content is returned.
By using the ETag, you can save traffic and keep local resources up to date.

serverEtag = headers.get("ETag");

serverdate
Server time, removed from the header.

headerValue = headers.get("Date");

Server time is taken to prevent problems caused by client time modification.

lastmodified
Again, this value is also issued on the server, telling the client when the last content changed.

headerValue = headers.get("Last-Modified");

Interact in the same way as the ETag

The first time the request is initiated, the server returns

Last-ModifiedFriMay 2006 18:53:33GMT

The second time the request is initiated, the client takes the

If-Modified-Since12200618:53:33 GMT

Similarly, if the page data does not change, 304 is returned

The reader may ask, the two kinds of species are OK, why define two. My understanding is that last_modified is not valid for dynamic Web pages. In addition, more than one way, in the use of the scene will be much more flexible.

The

Ttl****softttl
says these two arguments are longer. You have to start with a httpheader called Cache-control.
in order to save time, I come here directly to the portal http://www.blogjava.net/dashi99/archive/2008/12/30/249207.html
All in all, these two values are the life cycle of the cache. Beyond this time, it will expire. The usage scenario for the
values is shown in the following code. TTL is less than the current time, then the direct expiration, when the Softttl is less than the current time, need to do a refresh operation.

       /** True if the entry is expired. */        publicbooleanisExpired() {            returnthis.ttl < System.currentTimeMillis();        }        /** True if a refresh is needed from the original data source. */        publicbooleanrefreshNeeded() {            returnthis.softTtl < System.currentTimeMillis();        }

The following is a procedure for obtaining these two values, where Finalexpire is Ttl,softexpire is Softttl.

        //Cache-control takes precedence over an Expires header, evenifBoth exist andExpires//  isMore restrictive.if(Hascachecontrol) {Softexpire = now + MaxAge * +; Finalexpire = mustrevalidate? Softexpire:softexpire + stalewhilerevalidate * +; }Else if(Serverdate >0&& serverexpires >= serverdate) {//Default Semantic forExpire HeaderinchHTTP specification isSoftexpire.            Softexpire = Now + (serverexpires-serverdate);        Finalexpire = Softexpire; }

The great thing about this cache is that it implements the caching method mentioned in HTTP completely, which is worth

HttpCache design of volley frame analysis (IV.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.