GUAC-1126 Add caching of GET requests in REST API services.

This commit is contained in:
James Muehlner
2015-04-08 22:35:18 -07:00
parent 5eb4ab99c6
commit 91b0d72a60
6 changed files with 165 additions and 24 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2014 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Service which contains all REST API response caches.
*/
angular.module('rest').factory('cacheService', ['$injector',
function cacheService($injector) {
// Required services
var $cacheFactory = $injector.get('$cacheFactory');
// Return service containing all caches
return {
/**
* Shared cache used by both connectionGroupService and
* connectionService.
*
* @type $cacheFactory.Cache
*/
connections : $cacheFactory('API-CONNECTIONS'),
/**
* Cache used by protocolService.
*
* @type $cacheFactory.Cache
*/
protocols : $cacheFactory('API-PROTOCOLS'),
/**
* Shared cache used by both userService and permissionService.
*
* @type $cacheFactory.Cache
*/
users : $cacheFactory('API-USER')
};
}]);

View File

@@ -23,9 +23,17 @@
/** /**
* Service for operating on connection groups via the REST API. * Service for operating on connection groups via the REST API.
*/ */
angular.module('rest').factory('connectionGroupService', ['$http', 'authenticationService', 'ConnectionGroup', angular.module('rest').factory('connectionGroupService', ['$injector',
function connectionGroupService($http, authenticationService, ConnectionGroup) { function connectionGroupService($injector) {
// Required services
var $http = $injector.get('$http');
var authenticationService = $injector.get('authenticationService');
var cacheService = $injector.get('cacheService');
// Required types
var ConnectionGroup = $injector.get('ConnectionGroup');
var service = {}; var service = {};
/** /**
@@ -65,6 +73,7 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
// Retrieve connection group // Retrieve connection group
return $http({ return $http({
cache : cacheService.connections,
method : 'GET', method : 'GET',
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroupID) + '/tree', url : 'api/connectionGroups/' + encodeURIComponent(connectionGroupID) + '/tree',
params : httpParameters params : httpParameters
@@ -97,6 +106,7 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
// Retrieve connection group // Retrieve connection group
return $http({ return $http({
cache : cacheService.connections,
method : 'GET', method : 'GET',
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroupID), url : 'api/connectionGroups/' + encodeURIComponent(connectionGroupID),
params : httpParameters params : httpParameters
@@ -133,9 +143,10 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
data : connectionGroup data : connectionGroup
}) })
// Set the identifier on the new connection group // Set the identifier on the new connection group and clear the cache
.success(function connectionGroupCreated(identifier){ .success(function connectionGroupCreated(identifier){
connectionGroup.identifier = identifier; connectionGroup.identifier = identifier;
cacheService.connections.removeAll();
}); });
} }
@@ -146,6 +157,11 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroup.identifier), url : 'api/connectionGroups/' + encodeURIComponent(connectionGroup.identifier),
params : httpParameters, params : httpParameters,
data : connectionGroup data : connectionGroup
})
// Clear the cache
.success(function connectionGroupUpdated(){
cacheService.connections.removeAll();
}); });
} }
@@ -173,6 +189,11 @@ angular.module('rest').factory('connectionGroupService', ['$http', 'authenticati
method : 'DELETE', method : 'DELETE',
url : 'api/connectionGroups/' + encodeURIComponent(connectionGroup.identifier), url : 'api/connectionGroups/' + encodeURIComponent(connectionGroup.identifier),
params : httpParameters params : httpParameters
})
// Clear the cache
.success(function connectionGroupDeleted(){
cacheService.connections.removeAll();
}); });
}; };

View File

@@ -23,9 +23,14 @@
/** /**
* Service for operating on connections via the REST API. * Service for operating on connections via the REST API.
*/ */
angular.module('rest').factory('connectionService', ['$http', 'authenticationService', angular.module('rest').factory('connectionService', ['$injector',
function connectionService($http, authenticationService) { function connectionService($injector) {
// Required services
var $http = $injector.get('$http');
var authenticationService = $injector.get('authenticationService');
var cacheService = $injector.get('cacheService');
var service = {}; var service = {};
/** /**
@@ -52,6 +57,7 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
// Retrieve connection // Retrieve connection
return $http({ return $http({
cache : cacheService.connections,
method : 'GET', method : 'GET',
url : 'api/connections/' + encodeURIComponent(id), url : 'api/connections/' + encodeURIComponent(id),
params : httpParameters params : httpParameters
@@ -108,6 +114,7 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
// Retrieve connection parameters // Retrieve connection parameters
return $http({ return $http({
cache : cacheService.connections,
method : 'GET', method : 'GET',
url : 'api/connections/' + encodeURIComponent(id) + '/parameters', url : 'api/connections/' + encodeURIComponent(id) + '/parameters',
params : httpParameters params : httpParameters
@@ -144,9 +151,10 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
data : connection data : connection
}) })
// Set the identifier on the new connection // Set the identifier on the new connection and clear the cache
.success(function connectionCreated(identifier){ .success(function connectionCreated(identifier){
connection.identifier = identifier; connection.identifier = identifier;
cacheService.connections.removeAll();
}); });
} }
@@ -157,6 +165,11 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
url : 'api/connections/' + encodeURIComponent(connection.identifier), url : 'api/connections/' + encodeURIComponent(connection.identifier),
params : httpParameters, params : httpParameters,
data : connection data : connection
})
// Clear the cache
.success(function connectionUpdated(){
cacheService.connections.removeAll();
}); });
} }
@@ -184,6 +197,11 @@ angular.module('rest').factory('connectionService', ['$http', 'authenticationSer
method : 'DELETE', method : 'DELETE',
url : 'api/connections/' + encodeURIComponent(connection.identifier), url : 'api/connections/' + encodeURIComponent(connection.identifier),
params : httpParameters params : httpParameters
})
// Clear the cache
.success(function connectionDeleted(){
cacheService.connections.removeAll();
}); });
}; };

View File

@@ -23,11 +23,19 @@
/** /**
* Service for operating on user permissions via the REST API. * Service for operating on user permissions via the REST API.
*/ */
angular.module('rest').factory('permissionService', ['$http', 'authenticationService', 'PermissionPatch', angular.module('rest').factory('permissionService', ['$injector',
function permissionService($http, authenticationService, PermissionPatch) { function permissionService($injector) {
var service = {}; // Required services
var $http = $injector.get('$http');
var authenticationService = $injector.get('authenticationService');
var cacheService = $injector.get('cacheService');
// Required types
var PermissionPatch = $injector.get('PermissionPatch');
var service = {};
/** /**
* Makes a request to the REST API to get the list of permissions for a * Makes a request to the REST API to get the list of permissions for a
* given user, returning a promise that provides an array of * given user, returning a promise that provides an array of
@@ -49,6 +57,7 @@ angular.module('rest').factory('permissionService', ['$http', 'authenticationSer
// Retrieve user permissions // Retrieve user permissions
return $http({ return $http({
cache : cacheService.users,
method : 'GET', method : 'GET',
url : 'api/users/' + encodeURIComponent(userID) + '/permissions', url : 'api/users/' + encodeURIComponent(userID) + '/permissions',
params : httpParameters params : httpParameters
@@ -211,8 +220,12 @@ angular.module('rest').factory('permissionService', ['$http', 'authenticationSer
url : 'api/users/' + encodeURIComponent(userID) + '/permissions', url : 'api/users/' + encodeURIComponent(userID) + '/permissions',
params : httpParameters, params : httpParameters,
data : permissionPatch data : permissionPatch
})
// Clear the cache
.success(function permissionsPatched(){
cacheService.users.removeAll();
}); });
}; };
return service; return service;

View File

@@ -23,9 +23,14 @@
/** /**
* Service for operating on protocol metadata via the REST API. * Service for operating on protocol metadata via the REST API.
*/ */
angular.module('rest').factory('protocolService', ['$http', 'authenticationService', angular.module('rest').factory('protocolService', ['$injector',
function protocolService($http, authenticationService) { function protocolService($injector) {
// Required services
var $http = $injector.get('$http');
var authenticationService = $injector.get('authenticationService');
var cacheService = $injector.get('cacheService');
var service = {}; var service = {};
/** /**
@@ -46,6 +51,7 @@ angular.module('rest').factory('protocolService', ['$http', 'authenticationServi
// Retrieve available protocols // Retrieve available protocols
return $http({ return $http({
cache : cacheService.protocols,
method : 'GET', method : 'GET',
url : 'api/protocols', url : 'api/protocols',
params : httpParameters params : httpParameters

View File

@@ -26,13 +26,14 @@
angular.module('rest').factory('userService', ['$injector', angular.module('rest').factory('userService', ['$injector',
function userService($injector) { function userService($injector) {
// Required services
var $http = $injector.get('$http');
var authenticationService = $injector.get('authenticationService');
var cacheService = $injector.get('cacheService');
// Get required types // Get required types
var UserPasswordUpdate = $injector.get("UserPasswordUpdate"); var UserPasswordUpdate = $injector.get("UserPasswordUpdate");
// Get required services
var $http = $injector.get("$http");
var authenticationService = $injector.get("authenticationService");
var service = {}; var service = {};
/** /**
@@ -63,6 +64,7 @@ angular.module('rest').factory('userService', ['$injector',
// Retrieve users // Retrieve users
return $http({ return $http({
cache : cacheService.users,
method : 'GET', method : 'GET',
url : 'api/users', url : 'api/users',
params : httpParameters params : httpParameters
@@ -90,6 +92,7 @@ angular.module('rest').factory('userService', ['$injector',
// Retrieve user // Retrieve user
return $http({ return $http({
cache : cacheService.users,
method : 'GET', method : 'GET',
url : 'api/users/' + encodeURIComponent(username), url : 'api/users/' + encodeURIComponent(username),
params : httpParameters params : httpParameters
@@ -115,13 +118,19 @@ angular.module('rest').factory('userService', ['$injector',
token : authenticationService.getCurrentToken() token : authenticationService.getCurrentToken()
}; };
// Retrieve user // Delete user
return $http({ return $http({
method : 'DELETE', method : 'DELETE',
url : 'api/users/' + encodeURIComponent(user.username), url : 'api/users/' + encodeURIComponent(user.username),
params : httpParameters params : httpParameters
})
// Clear the cache
.success(function userDeleted(){
cacheService.users.removeAll();
}); });
}; };
/** /**
@@ -142,12 +151,17 @@ angular.module('rest').factory('userService', ['$injector',
token : authenticationService.getCurrentToken() token : authenticationService.getCurrentToken()
}; };
// Retrieve user // Create user
return $http({ return $http({
method : 'POST', method : 'POST',
url : 'api/users', url : 'api/users',
params : httpParameters, params : httpParameters,
data : user data : user
})
// Clear the cache
.success(function userCreated(){
cacheService.users.removeAll();
}); });
}; };
@@ -170,12 +184,17 @@ angular.module('rest').factory('userService', ['$injector',
token : authenticationService.getCurrentToken() token : authenticationService.getCurrentToken()
}; };
// Retrieve user // Update user
return $http({ return $http({
method : 'PUT', method : 'PUT',
url : 'api/users/' + encodeURIComponent(user.username), url : 'api/users/' + encodeURIComponent(user.username),
params : httpParameters, params : httpParameters,
data : user data : user
})
// Clear the cache
.success(function userUpdated(){
cacheService.users.removeAll();
}); });
}; };
@@ -205,7 +224,7 @@ angular.module('rest').factory('userService', ['$injector',
token : authenticationService.getCurrentToken() token : authenticationService.getCurrentToken()
}; };
// Retrieve user // Update user password
return $http({ return $http({
method : 'PUT', method : 'PUT',
url : 'api/users/' + encodeURIComponent(username) + '/password', url : 'api/users/' + encodeURIComponent(username) + '/password',
@@ -214,6 +233,11 @@ angular.module('rest').factory('userService', ['$injector',
oldPassword : oldPassword, oldPassword : oldPassword,
newPassword : newPassword newPassword : newPassword
}) })
})
// Clear the cache
.success(function passwordChanged(){
cacheService.users.removeAll();
}); });
}; };