The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer::Plugin::DynamicConfig

SYNOPSIS

In your Dancer config.yml:

  plugins:
    DynamicConfig:
        active_sessions: "active_sessions.json"
        contacts:
          path: "etc/contact_info.json"
          rewrite_class: "ContactInfoRewriter"

In your Dancer application:

  use Dancer::Plugin::DynamicConfig;

  my $sessions = dynamic_config('active_sessions');
  my $contacts = dynamic_config('contacts');

  if (not $sessions->{$session_id}) {
    redirect $login_page;
    return;
  }

  my $user_id = $request->cookies->{user_id};
  if ($contacts->{$user_id}{is_platinum_user}) {
    $allow_platinum_features = 1;
  }

DESCRIPTION

Dancer::Plugin::DynamicConfig provides a simple and efficient means to read and decode the contents of a file which might change while your Dancer application is running.

In your Dancer configuration, you declare a "tag" for the file, a path to the file, and an (optional) rewrite_class. You can then call dynamic_config(), passing in your tag, and you will receive back a data structure that represents the contents of the file on disk. dynamic_config() will cache the data, and only re-read it when the file's mtime has changed.

The rewrite_class

This class, if provided, must implement one class method, rewrite(), which takes the decoded data structure represented by the file's contents. rewrite() may then return any data structure it likes, and this structure will be passed back as the return value of dynamic_config().

Filetypes

Currently, Dancer::Plugin::DynamicConfig only supports JSON files, and requires that the filename end in ".json". Generalizing this behavior is on the short list of coming improvements.

KEYWORDS

dynamic_config($config_key)

METHODS

reinitialize

Forces a cache flush and re-read of the Dancer configuration and all dynamic_config files.

NOTES

This plugin uses Time::HiRes::stat() to detect the mtimes of your configuration files. Timestamp resolution is limited by not only your operating system, but also the filesystem you are accessing. In particular, Mac OS X HFS will usually have a timestamp resolution of one second. On such filesystems, if the file is updated, e.g., twice in one second, there is a potential race condition in which you will cache the results of the first update and not realize that there has been a second update.

AUTHOR

Kurt Starsinic <kstarsinic@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Kurt Starsinic. It was originally authored at and for Shutterstock, Inc., which has graciously allowed this code to be made publicly available.

This module is free software; you can redistribute and/or modify it under the same terms as perl 5.18.1.

SEE ALSO

Dancer, Dancer::Plugin, JSON::XS, Time::HiRes