Managing cache tiles with the GWC REST API

Managing the cache is a more complex task than just managing the layer configuration. Cache is composed of map tiles that you may create at once by seeding the layer or waiting for the users to issue GetMap requests that have gone through the GeoWebCache and are stored as maps tile in the GeoWebCache repository.

The GWC REST interface supports the seeding, truncating, and managing tiles of cached layers.

How to do it…

  1. Create a new XML file, insert the following code, and save it as seedCoastline.xml:
    <seedRequest>
      <name>NaturalEarth.Coastline</name>
      <srs>
        <number>4326</number>
      </srs>
      <zoomStart>1</zoomStart>
      <zoomStop>8</zoomStop>
      <format>image/png</format>
      <type>seed</type>
      <threadCount>4</threadCount>
    </seedRequest>
  2. Send a cURL request to start seeding for the NaturalEarth:Coastline layer:
    $ curl -u admin:geoserver -XPOST -H 'Content-type: text/xml' -d @seedCoastline.xml http://localhost:8080/geoserver/gwc/rest/seed/NaturalEarth:Coastline.xml
    
  3. Alternatively, do the same operation in Python:
    >>> myUrl = 'http://localhost:8080/geoserver/gwc/rest/seed/NaturalEarth:Coastline.xml'
    >>> file = open('seedCoastline.xml','r')
    >>> payload = file.read()
    >>> headers = {'Content-type': 'text/xml','Accept': 'text/xml'}
    >>> resp = requests.post(myUrl, auth=('admin','geoserver'), data=payload, headers=headers)
    >>> resp.status_code
    200
    
  4. Now, go to the tile layers list and press the Seed/Truncate link for the NaturalEarth:Coastline layer. You should see four listed processes that are calculating tiles for you, as shown in the following screenshot:
    How to do it…

How it works…

Map tiles need to be created and then saved on filesystems, and sometimes may require a lot of disk space and a lot of time.

To check the status of the seed operation, you can use the web interface as in the previous screenshot, but we prefer to leverage on REST. The code is as follows:

$ curl -u admin:geoserver -XGET http://localhost:8080/geoserver/gwc/rest/seed/NaturalEarth:Coastline.json -o seedingProcesses.json

This returns a list of running process for a specific layer:

{"long-array-array":[[3072,167596,581,7,1],[4592,167596,374,8,1],[5840,167596,290,9,1],[2640,167596,684,10,1]]}

The JSON file contains an array of arrays. Each array corresponds to a single seeding/truncating task. The meaning of each long value in each thread array is shown as follows:

[tiles processed, total number of tiles to process, estimated number of second to end, Task ID, Task status]

The task status value returned will be one of the following:

  • -1 = ABORTED
  • 0 = PENDING
  • 1 = RUNNING
  • 2 = DONE

So, in our case, we have something like this:

Tiles processed

Total tiles to process

Seconds to the end

Task ID

Task status

3072

167596

581

7

RUNNING

4592

167596

374

8

RUNNING

5840

167596

290

9

RUNNING

2640

167596

684

10

RUNNING

Of course, it is always possible to totally halt the seeding process with a simple GET request. It can be issued to a single layer, as shown in the following code snippet. This halts all processes running for the NaturalEarth.Coastline layer:

$ curl -u admin:geoserver -XGET -d 'kill_all=all' http://localhost:8080/geoserver/gwc/rest/seed/NaturalEarth:Coastline

You have several processes that are creating tiles for many layers. If you need to stop them all, the REST interface gives you an operation to stop them all. Regardless of the layer, you shall use /gwc/rest/seed as the entry point. The following line of code halts all seeding and truncating processes on the server:

$ curl -u admin:geoserver -XGET -d 'kill_all=all' http://localhost:8080/geoserver/gwc/rest/seed

You can also erase all tiles for a layer; this may be useful when your data needs to be updated and the cache is no more useful. Just prepare an XML file similar to that previously prepared for the seeding and insert truncate as the operation type, as shown in the following example:

<seedRequest>
  <name>NaturalEarth.Coastline</name>
  <srs>
    <number>4326</number>
  </srs>
  <zoomStart>1</zoomStart>
  <zoomStop>8</zoomStop>
  <format>image/png</format>
  <type>truncate</type>
  <threadCount>4</threadCount>
</seedRequest>

Then, send it to GeoServer and all processes will be halted:

$ curl -u admin:geoserver -XPOST -H 'Content-type: text/xml' -d @seedStopCoastline.xml http://localhost:8080/geoserver/gwc/rest/seed/NaturalEarth:Coastline.xml
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.45