1

I need to to refresh the cache after i add new data to database.

angular.module('app').factory('myService', function ($http,$cacheFactory) {

  var profileCache=$cacheFactory('profiles');

  //get list of items
  function getItem(){
   var request=$http({
     method:'get',
     url:domain+'/api/v1/items/list',
     cache:profileCache,
     params:{
       action:'get'
     }
   });
   return(request.then(response.data));
 }
// Post items
 function addItem(item){
   var request=$http({
     method:'post',
     url:domain+'/api/v1/items/add',
     data:item
   });
   return(request.then(response.data));
 }
})

I want to refresh the cache once the item is added,so that the cache can have new changed data.

Bikash Ramtel
  • 115
  • 1
  • 3
  • 12

2 Answers2

0

as you can read here

https://docs.angularjs.org/api/ng/service/$http

$http responses are not cached by default

but if you have particular environment and you need to prevent any caching you can add a date time to your request example

function getItem(){
   var d = new Date();
   var n = d.getDate();
   var request=$http({
     method:'get',
     url:domain+'/api/v1/items/list?' + n,
     cache:true,
     params:{
       action:'get'
     }
   });
   return(request.then(response.data));
 }
danilonet
  • 1,757
  • 16
  • 33
-1

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

Michal Kucaj
  • 681
  • 5
  • 15