tineikt / xp-lib-perfectcache-headers

lib and util to set cache headers assisting varnish in purging XP contents

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TINE Logo xp-lib-perfectcache-headers

What is this

This is a util lib that helps create and handle the xkey headers used by xkey vmod in Varnish to support secondary hashes to objects.

Setting up reference to this lib in your own project

  1. Add lib as a dependency to your build.gradle file
dependencies {
	include 'com.github.tineikt:xp-lib-perfectcache-headers:1.0.0'
}
  1. Add response filter to your site.xml
<site>
	...
	<filters>
		<response-filter name="perfectCacheHeaders" order="10"/>
	</filters>
</site>

Usage

Require it in your JavaScript code.

import { pch } from '/lib/tineikt/perfectcache-headers';

Create a new perfect cache header object.

const cacheHeaders = new pch(name);

name should be something unique for the complete request. If used in a part we could use the path to the component.

We can then populate this cache by adding relations to it in our controller.

cacheHeaders.add(content._id);
cacheHeaders.add(content.data.conRelatedContents);

Then all we have to do is make sure we return the headers in the response.

const headers = cacheHeaders.getHeader();
return {
	headers,
	body: freemarker.render(viewFile, model)
};

xp-content-path

The xp-content-path header should also be set on page controllers with the value of the path to the content. This header is used by the xp-app-perfectcache-purger to ban whole whole sites/trees.

headers['xp-content-path'] = encodeURI(content._path); // if paths contain åæø or other chars invalid in header values.

Methods

getHeader()

Returns a simple header ready to be returned in response

{
	headers: {
		pch-name: 'con-ce5dc0e7-0e28-47fc-b4a6-4d81727f00e7,cat-104c0db2-aa53-411c-8f6a-9213947d13d9'
	}
}

add(guid, type = 'con-')

Adds a single content to the header

remove(guid, type = 'con-')

Remove a single content from the header

utilMedia(media)

Util function to support media mixin and add selected image or video content by passing media node

utilTarget(url)

Util function to support link mixin and add selected target content if internal link

utilFragments(region)

Util function iterates a region to find any fragment in that region

utilSelectorConfig(config, contents)

Util function using the list config to fetch content, section and tags affecting the resulting resultset.

About

lib and util to set cache headers assisting varnish in purging XP contents


Languages

Language:JavaScript 100.0%