Git Product home page Git Product logo

demo_http_cache's Introduction

#

DemoHttpCache

Service $http có thể tạo một cache với ID là $http với thao tác khá đơn giản là set "cache: true" khi thực hiện requets thông qua $http.

$http.get('/users.json', {
  cache: true
});

bằng cách đó mọi requets tới '/users.json' để được lưu lại trong cache thông qua cache object có tên là "$http"

Để lấy ra cache của $http ta vẫn dùng phương thức get của $cacheFactory:

var cache = $cacheFactory.get('$http');

Giá trị trong $http cache được lưu theo dạng:

Key là url trong request của $http Value là content của response trả về.

Ví dụ

  • Tôi tạo 1 controller để load danh sách users
myApp.controller("UserListCtr", ['$scope', '$http', '$resource', 'Users', 'User', '$location', function($scope, $http, $resource, Users, User, $location) {

  $scope.loadUsers = function() {
    // alert("Aready got data from sever");
    $http.get("http://localhost:9000/users.json").success(function(data) {
      $scope.users = data;
    }).error(function(){});
  };
  $scope.loadUsers();
}]);

Bởi mặc định thì cache cho $http được set là false

$http.get("http://localhost:9000/users.json")
  • Tạo 1 'button' trên view để gọi function 'loadUsers'
<button ng-click="loadUsers()">load users</button>

Mỗi lần nhấn "load users" $http sẽ thực hiện 1 request đên controler:

image1

image2

Bây giờ, set cache thành true:

$http.get("http://localhost:9000/users.json", {cache:true})

khi nhấn "load users" sẽ không reuqest lên server nữa.

Như đã nói ở trên key của $http cache là url của requet vì vậy ta có thể get thông tin của nó thông qua key này:

$scope.removeHttpCache = function(){
    var userCache = $cacheFactory.get('$http');
    console.log("userCache: ", userCache.get("http://localhost:9000/users.json"));
    userCache.remove("http://localhost:9000/users.json")
  };
  • tạo nút remove cache và bấm vào:
<button ng-click="removeHttpCache()">remove cache</button>

kết quả là khi bấm vào lần thứ nhất console hiển thị userCache và sau đó xóa cache đi. Tiếp tục bấm vào thì userCache sẽ undefine bởi vì nó đã bị xóa đi.

image3

###Custome $http cache

Mặc định $http cache sẽ có ID là "$http" nhưng bạn cũng có thể thay đổi nó. bằng cách sau:

myApp.factory("usersCache", function($cacheFactory){
  return $cacheFactory('usersCached');
});

và thay đổi thuộc tính cache: true thành cache:usersCache như vậy thay vì sử dụng default cache thì $http sẽ sử dụng cache được custome của bạn "usersCache"

demo_http_cache's People

Contributors

hoangquan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.