Guest User

Untitled

a guest
Jun 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. //...
  2.  
  3. function getData(request) {
  4. // ...
  5.  
  6. var startDate = request.dateRange.startDate;
  7. var endDate = request.dateRange.endDate;
  8.  
  9. var cache = new DataCache(CacheService.getUserCache(), startDate, endDate);
  10. var plays = null;
  11. plays = fetchFromCache(cache);
  12. if (!plays) {
  13. plays = fetchFromApi(startDate, endDate);
  14. setInCache(plays, cache);
  15. }
  16.  
  17. return buildTabularData(plays, dataSchema);
  18. }
  19.  
  20. function prepareSchema(request) {
  21. // Prepare the schema for the fields requested.
  22. var dataSchema = [];
  23. var fixedSchema = getSchema().schema;
  24. request.fields.forEach(function(field) {
  25. for (var i = 0; i < fixedSchema.length; i++) {
  26. if (fixedSchema[i].name == field.name) {
  27. dataSchema.push(fixedSchema[i]);
  28. break;
  29. }
  30. }
  31. });
  32.  
  33. return dataSchema;
  34. }
  35.  
  36. function fetchFromCache(cache) {
  37. var plays = null;
  38. console.log('Trying to fetch from cache...');
  39. try {
  40. var playsString = cache.get();
  41. plays = JSON.parse(playsString);
  42. console.log('Fetched succesfully from cache', plays.length);
  43. } catch (e) {
  44. console.log('Error when fetching from cache:', e);
  45. }
  46.  
  47. return plays;
  48. }
  49.  
  50. function setInCache(plays, cache) {
  51. console.log('Setting data to cache...');
  52. try {
  53. cache.set(JSON.stringify(plays));
  54. } catch (e) {
  55. console.log('Error when storing in cache', e);
  56. }
  57. }
Add Comment
Please, Sign In to add comment