Check out the latest documentation.

What's Covered

This tutorial illustrates how to create a 51Degrees device detection dataset with a custom caching configuration.

Code and Explanation

This Example shows how to:

  1. use DataSetBuilder to supply a custom cache configuration to the 51 Degrees device detection API.
          Dataset dataset = DatasetBuilder.file()
              .setCacheBuilder(NodesCache, builder)
              .setCacheBuilder(ProfilesCache, null)
              .lastModified(new Date())
Full Source File
    public static void main (String[] args) throws IOException {
        // Create the guava cache builder for the non-user agent caches.
        ICacheBuilder builder = new GuavaCacheBuilder();

        // Create the dataset using DatasetBuilder
        Dataset dataset = DatasetBuilder.file()
                // First, use the configuration from the MultiThreadLowMemory template.
                // Next, set the signature cache to use the Guava cache builder.
                .setCacheBuilder(SignaturesCache, builder)
                // Set the profile cache to null, this means that profiles will not
                // be stored in memory and will always be loaded from disk when needed.
                .setCacheBuilder(ProfilesCache, null)
                .lastModified(new Date())

        // Create a new guava cache to hold user agents and their corresponding match objects. uaCache = CacheBuilder.newBuilder()
                .concurrencyLevel(5) // set to number of threads that can access cache at same time

        // Create the provider using the guava cache we've just created.
        Provider provider = new Provider(dataset, new UaCacheAdaptor(uaCache));

        // Use the provider to obtain a match on a user agent
        // User-Agent string of a iPhone mobile device.
        String mobileUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone "
                + "OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) "
                + "Version/7.0 Mobile/11D167 Safari/9537.53";

        Match match = provider.match(mobileUserAgent);
        System.out.printf("%s", match.getSignature());

Full Source File


In this tutorial you have seen how to use DataSetBuilder to create a dataset with a custom cache configuration.

The example uses the Guava cache but the same principle could be applied to any 3rd party cache implementation.

The 51 Degrees API has been tested extensively to determine the best caching strategies and parameters for a wide range of environments and use cases. Care should be taken when using a custom configuration to ensure that performance is not degraded unintentionally.