UGC Techdocs
  • Introduction
  • Guides
    • Analytics
      • Tracking UGC on Adobe Analytics
      • Tracking Widget interactions with Google Analytics
    • Direct Uploader
      • How to add Custom Data to Direct Uploader
      • How to capture Custom Data on Direct Uploader
      • How to automatically tag data on Direct Uploader
      • How to Customize error messages on Direct Uploader
      • How to Track Direct Uploader form submissions with Google Analytics (Universal & GA 4)
    • Data Templates
      • Creating a Data Template
    • Rights via Registration
      • Capture Custom Data on Rights by Registration Form
      • Styling Rights via Registration Form
    • REST API
      • Caching REST API results for optimization
      • Posting content into Nosto via Tile API
      • Posting images into Nosto via Tile API
    • Onsite Widgets (2.0)
      • Blank Canvas
        • How to Use the Blank Canvas to Create a Twitter Count Widget
        • Creating an Auto-Scrolling Carousel using Blank Canvas
        • Creating Gallery Widget by Using the Blank Canvas Widget
        • Creating a simple Hover effect using Blank Canvas
        • Creating a Word Cloud using Blank Canvas
        • Creating Your Widget by Using the Blank Canvas
      • Bind your own Events
      • Creating a Grid Widget from Waterfall
      • Create a Q&A Widget using Data Templates
      • Displaying a Widget in a Mobile App
      • Dynamically Specify Products to Display in Widget
      • Dynamically specify what Tile to display in a Widget
      • How to add a title / subtitle to a widget
      • How to localize the load more button on widgets
      • How to overlay existing Google Map with the UGC Map Widget
      • Styling cross-sellers on Grid and Carousel Widgets
      • How to Load External JS and CSS into Widgets
      • Profiling Widget Performance
      • Re-targeting with Widgets and Facebook Pixel
      • Render Widget filters dynamically
      • Styling Carousel Widget
      • Styling Grid Widget
      • Styling Masonry Widget
      • Styling Waterfall Widget
      • Styling Widget Expanded Tile
      • Styling Widget Shopspots
      • Using Web Fonts in Widgets
    • Digital Screens
      • Customizing Carousel Event Screen
      • Customizing Mosaic Event Screen
      • Customizing Scrollwall Event Screen
      • Customizing the Mosaic Event Screen to Have 9 Even Tiles
    • Email
      • Adding Location to an Email Tile
      • Styling the Email Widget
    • Integrations
      • DoubleClick
        • UGC Ads with Nosto and Google DoubleClick
      • Zapier
        • Consuming UGC Webhooks via Zapier
      • Mailchimp
        • Bring Social Content into a Mailchimp Campaign
    • Webhooks
      • Trigger notifications when content is in the moderation queue
  • Widgets
  • API Docs
    • JavaScript API
      • Widgets
        • Introduction
        • API Reference for Content Widgets
        • API Reference for Blank Canvas
        • API Reference for Map Widget
      • Digital Screens
        • Introduction
        • API Reference
    • Content API
      • Reference
    • REST API
      • Reference
        • Filters API
        • Moderation Views API
        • Tags API
        • Terms API
        • Tiles API
        • Users API
        • Widgets API
        • Automation Rules API
        • REST API Reference Widgets style and config
      • Best Practices
    • Webhooks
  • Enterprise Tools
    • Automation Rules
      • Triggers
      • Actions
      • Samples
    • Data Templates
    • User Access Control (UAC)
    • Single Sign On (SSO)
    • Enterprise Admin User Interface (EAUI)
    • Zapier
  • Commerce Tools
    • Product Feeds
    • Widget Implementation
    • Reporting
    • Integrations
      • Google Tag Manager
      • Magento
      • SalesForce Commerce Cloud
      • Shopify
      • Shopify Add To Cart
        • Global Variant Mapping for Add to Cart
        • Customise Add to Cart Widget Experience
  • Analytics
    • Google Analytics 4
      • Getting Started
      • Widgets Events
      • E-commerce Events
      • Email Events
  • Terms of Use
Powered by GitBook
On this page
  • Overview
  • A note on cache methods
  • Key Concepts
  • memcached
  • Filters API
  • Example Application
  • The Fun Part
  • Install and configure memcached
  • Generate uncached requests
  • Generate cached requests
  • Summary and Next Steps

Was this helpful?

  1. Guides
  2. REST API

Caching REST API results for optimization

PreviousREST APINextPosting content into Nosto via Tile API

Last updated 11 months ago

Was this helpful?

Overview

When data fetched from Nosto's UGC via API is required for every page view, it can be expensive to request every impression. Often the data does not need to be the latest, especially if it's known that the data does not change often. In this guide, we will explore methods to reduce API consumption (reducing the risk of exceeding the API rate limit), while utilizing the data at the same time.

By using the cache method, we will end up having a faster web response and REST API's rate limits will not limit us to serve our busy web page to clients.

In this guide, we will use Memcached in a demo PHP application to implement a caching strategy for Filter data.

A note on cache methods

There are several cache methods that we can use and your mileage will certainly vary.

Most languages and frameworks have similar patterns for caching locally (e.g. local files or RAM such as or ), as well as drivers for distributed caching systems such as . Further to this, edge server caching applications such as can cache the result before it even hits your web server, or web server extensions can add the capability to Apache or nginx.

In each of these, the pattern is similar: rather than hitting the Nosto's UGC API every time data is required from a resource, store the response for some time in a cache and use the cached version until that period expires for all requests.

In the case of local cache, the data is stored on each web server. While good for single-server implementations, once you exceed one application server, and since each server will have its own cache, you run the risk of using different versions in different requests.

Distributed cache systems such as memcached resolve the above problem and allow a single cache to be accessed from multiple sources. For example, each one of your 5 application servers can have their application access the identical source for the cached data.

If you wish to step to a higher level, you can store the web server response itself in a cache and serve that to visitors via your web server or reverse proxy of some kind.

Key Concepts

memcached

Memcached is a general-purpose distributed memory caching system.

Filters API

Filters API provides an endpoint to obtain the filters that are available in the Stack.

Example Application

In this application, we will utilize memcached to cache the response of Nosto's UGC API in raw format and generate an output of it.

The Fun Part

In this guide we will do the following:

  • Install and configure memcached

  • Generate uncached requests

  • Generate cached requests

Install and configure memcached

Generate uncached requests


<?php
// Fetch filters from Nosto's UGC REST API
$filters = getFiltersFromStackla();
echo '<select name="filters">';
foreach ($filters as $id => $name) {
    echo "<option value=\"{$id}\">{$name}</option>";
}
echo '</select>';
// The End

Execute the above code several times, and you will notice that every request generates hits on the Nosto's UGC API, and the API console will indicate that there are now fewer requests available.

Generate cached requests

Note on getFiltersFromStackla()

Connect to and query Memcached

The following code is a sample of how to connect to the memcached server.

$mc = new Memcache();
// Modify this for your server
$mcHost = 'localhost';
$mcPort = 11211;
$mcPresist = true;
$mcWeight = 1;
$mcTimeout = 1;

// Add a Memcached server to the connection pool
$mc->addServer($mcHost, $mcPort, $mcPresist, $mcWeight, $mcTimeout);

// Connect to Memcached server
$mc->connect($mcHost, $mcPort);

Now that we have a connection, we can query for particular data, and if not found, store the data itself. memcached is a key-value store, so we will need a unique key to store the data.

if ($mc->get('filters')) {
    // Get cached value
    $filters = $mc->get('filters');
} else {
    // Fetch filters from Nosto's UGC REST API
    $filters = getFiltersFromStackla();
    // Cache filters for the next 30 seconds
    $mc->set('filters', $filters, time() + 30);
}

<?php
$mHost = 'localhost';
$mPort = 11211;
$mPresist = TRUE;
$mWeight = 1;
$mTimeout = 1;
$m = new Memcache();
// Add a Memcached server to the connection pool
$m->addServer ( $mHost, $mPort, $mPresist, $mWeight, $mTimeout);
// Connect to the Memcached server
$m->connect($mHost, $mPort);
// Check if the filters result is in Memcached
if ($m->get('filters')) {
    // Get cached value
    $filters = $m->get('filters');
} else {
    // Fetch filters from Nosto's UGC REST API
    $filters = getFiltersFromStackla();
    // Cache filters for 30 seconds minute
    $m->set('filters', $filters, time() + 30);
}
echo '<select name="filters">';
foreach ($filters as $id => $name) {
    echo "<option value=\"{$id}\">{$name}</option>";
}
echo '</select>';
// The End

After executing this code, you will notice that the first request reduces the allowed requests in the console, however for the following 30 seconds, subsequent hits do not. This is the effect of the cache.

Summary and Next Steps

This example can be further expanded with the following activities:

  • Caching other Nosto's UGC endpoints

  • Caching Filter content after dynamically changing Filter settings (personalized content)

In this guide we won't be going through installing it as it comes in many variants, however, if you're not familiar there is great info on the Memcached and .

Before we do anything, we assume that there is a function named getFiltersFromStackla that retrieves the raw JSON from the Stack and processes it into an associative array (id=>name). Details on how to acquire the Filters are covered in , although the effort to wrap the task in the getFiltersFromStackla is left as a separate exercise.

PHP-APC
node-cache
memcached
Varnish
Wiki
PHP Manual
this guide