AngularJS and AngularJS Cache

Source: Internet
Author: User

AngularJS and AngularJS Cache

Cache

A cache is a component that transparently stores data so that it can serve requests more quickly. Repeated resource acquisition may result in data duplication and time consumption. Therefore, the cache is suitable for some data with little variability. The more requests the cache can serve, the higher the overall system performance.

$ CacheFactory and cache object

$ CacheFactory is a service that produces cache objects for Angular services. To create a cache object, you can use $ cacheFactory through an ID and capacity. Here, ID is the name of a cache object, and capacity is the maximum number of key-value pairs that describe the cache. For example, $ cacheFactory is a leasing lady. She has a building in which houses, large and small, can be rented. If you have enough money, the package will rent the house to you (get the cache object). The house includes its room number (ID) and room size (capacity-capacity ).

var myCache = $cacheFactory('myCache');

Cache objects have the following methods:

1. myCache.info () returns the cache Object ID, size, and options

2. myCache. put () New Value key-Value Pair and put it into the cache object myCache. put ("name", "Ben ")

3. myCache. get () returns the corresponding cache value. If no value is found, undefined myCache. get ("name") is returned ")

4. myCache. remove () remove the key-value pair from the corresponding cache object myCache. remove ("name ")

5. myCache. remvoeAll () clears the cache object

$ Cache in http

The $ http () method allows us to pass a cache parameter. The default $ http cache is especially useful when data does not change frequently. The default $ http cache object is var cache = $ cacheFactory ('$ http'). You can set it like this.

$http({   method: 'GET',   url: 'api/user.json',   cache: true})

The cached key value is url, var userCache = cache. get ('api/user. json ')

Custom Cache

It is also easy to initiate a request for $ http through a custom cache. You only need to set the cache value to the name of the corresponding cache object.

$http({   method: 'GET',   url: 'api/user.json',   cache: myCache})

You can also use config configuration to set the cache object for each $ http request without adding the configuration to each $ http request as in the preceding example.

app.config(function($httpProvider){$httpProvider.defaults.cache = $cacheFactory('myCache',{capacity: 20})

Among them, capacity will use the "recently used cache Algorithm for the longest time", that is, if the cache capacity is 20, 20 are already cached. When 21st want to be cached, the minimum unused cache key-value pairs are cleared to free up space for 21st caches.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.