<<

NAME

pf::config::cached

DESCRIPTION

pf::config::cached object is a proxy for a cached (in CHI) Config::IniFiles object

SYNOPSIS

By default, the pf::cached::object is internally cached and keyed by file path.

So multiple calls to pf::config::cached->new with the same file path will return the same object.

Simple usage

  use pf::config::cached;
  my $cached_config = pf::config::cached->new( -file => 'file.conf');

Using as an tied hash

  use pf::config::cached;
  tie my %Config, 'pf::config::cached' => (-file => 'file.conf');

Tying an hash using an existing pf::config::cached object

  use pf::config::cached;
  my $cached_config = pf::config::cached->new( -file => 'file.conf');
  tie my %Config, $cached_config;

Creating a pf::config::cached object with some callbacks

  use pf::config::cached;
  my $cached_config = pf::config::cached->new(
    -file => 'file.conf'
    -onreload => [
        onreload_config => sub { ...  },
    ],
    -onfilereload => [
        onfilereload_config => sub { ...  },
    ],
  );

BEST PRACTICES

Callbacks

Types of callbacks

onreload - called whenever the config is (re)loaded from the filesystem or the cache
onfilereload - called whenever the config is (re)loaded only from the filesystem

Behavior

The callbacks are triggered by calling ReadConfig and there has been a change in the cache or filesystem. $config->ReadConfig()

To reload all the configs that have been created call ReloadConfigs.

Call unloadConfig to avoid a config file from being re-read when ReloadConfigs is called. $config->unloadConfig()

The order callbacks are called

1) onreload callbacks in order of insertion
2) onfilereload callbacks in order of insertion

Adding callbacks

All callbacks are named to ensure the same callback is added only once.

At creation of object

All callbacks will be called after creating an object or recieving it from the cache. If recieved from the cache, it will replace any existing callbacks with the same name.

Example: $cached_config = pf::config::cached->new( -file => 'file.conf' -onreload => [ onreload_config => sub { ... }, ], -onfilereload => [ onfilereload_config => sub { ... }, ], );

Add callbacks to an existing pf::config::cached object

If the name already exists, it replaces the previous callback keeping its calling order.

When adding a callback to an existing pf::config::cached object it will not be called. If it needs to be called it must be done manually.

Adding new callback example: $cached_config->addReloadCallbacks( 'onreload_do_something_else' => sub {...} ); $cached_config->addFileReloadCallbacks( 'onfilereload_do_something_else' => sub {...} );

  my $callback = sub {...};
  $cached_config->addReloadCallbacks('callback_name' => $callback);
  $callback($cached_config,'callback_name');

Adding new callback then calling them after: my $callback = sub {...}; $cached_config->addReloadCallbacks('callback_name' => $callback); $callback($cached_config,'callback_name');

Removing callbacks

Currently not supported

Libraries

A quick guide on how to use in a library

Singleton

By default pf::config::cached are singleton so you only initialize them once.

They should be stored in a package variable.

Readonly

If you are only reading the data and not creating other data then these methods are safe to use:

val
exists
Sections
SectionExists
Parameters
Groups
GroupMembers
GetFileName

Modifing data in the config

If the library doesn't have to modify data in the cache, then never call any methods that modify data.

If the library has to modify data, then always perform the RewriteConfig at the latest time.

Setting up data

If the library is reading configuration data to setup global variables, then it must use callbacks to update its data when the config is reloaded.

Example: use pf::config::cached; my %hash; my = $cached_config = pf::config::cached->new( -file => 'file.conf' -onreload => [ onreload_config => sub { my ($config,$name) = @_; $config->toHash(\%hash); $config->cleanupWhitespace(\%hash); }, ], );

Reloading configurations

In general, reloading configurations should only happen at the start or end of an event loop to ensure the latest data is loaded. A good rule to follow for a function other than initialization is to not call $config-ReadConfig()> or ReloadConfigs().

Daemons

When to Reload

It is best to reload the data before the event loop begins or after the event loop ends.

Using the configuration

Copying to non-global variables

The safest way (but not the most effecient way) is to copy the configuration to a temporary variable.

Example: $config->toHash(\%hash);

Setting up global variables

If the daemon is reading configuration data to setup global variables, then it must use callbacks to update its data when the config is reloaded.

Example: use pf::config::cached; my %hash; my = $cached_config = pf::config::cached->new( -file => 'file.conf' -onreload => [ onreload_config => sub { my ($config,$name) = @_; $config->toHash(\%hash); $config->cleanupWhitespace(\%hash); }, ], );

HTML::FormHandler

Default values

If the default value is from a derived value then you should use a sub routine to set the default value

See "Defaults" in HTML::FormHandler::Manual::Defaults for more information

  package pfappserver::Form::Person;

  use HTML::FormHandler::Moose;
  extends 'HTML::FormHandler';
  with 'pfappserver::Form::Widget::Theme::Pf';

  sub default_hair { $Config{person}{default} eq 'James' ? 'no' : 'yes' }

  has_field hair =>
  (
    type => 'Toggle',
    wrapper => 'Switch',
    checkbox_value => 'yes',
    unchecked_value => 'no',
  );

Moose

Default values

When using a value that was dervived from a configuration use a sub routine to create the default value

  package pf::person;

  use Moose;

  has 'hair' => (
    is => 'ro',
    default => sub { $Config{person}{default} ne 'James'  },
  );

METHODS

new

Creates a new pf::config::cached

Accepts all the arguements from Config::IniFiles->new With the the following additional arguements

-file filename * required

The pathname of a file of the configuration file The file does need to exist This will the key used for store the cached config

-onreload [name => sub {...}]

This is an array ref that contains pairs of strings and sub references The sub reference is passed the pf::config::cached object and the name of the callback

-onfilereload [name => sub {...}]

This is an array ref that contains pairs of strings and sub references The sub reference is passed the pf::config::cached object and the name of the callback

RewriteConfig

Will rewrite the config using the filename passed to it, update the cache, and run the onreload and onfilereload callbacks if successful.

Rollback

Rollback to current version of config in the cache Reverting all current changes

_callCallbacks

Helper function for calling the callbacks

_callReloadCallbacks

Call all reload callbacks

_callFileReloadCallbacks

Call all the file reload callbacks

_callFileReloadOnceCallbacks

Call all the file reload callbacks that should be called only once

_callCacheReloadCallbacks

Call all the cache reload callbacks

_callPostReloadCallbacks

Call all reload callbacks

removeDefaultValues

Will removed all the default values in current config

lockFileForWriting

Locks the lock file for writing a file

lockFileForReading

Locks the lock file for reading a file

_lockFileFor

helper function for locking files

_makeFileLock

will create the name of the lock file

ReadConfig

Will reload the config when changed on the filesystem and call any register callbacks

_swap_data

Swap the data with cached data

TIEHASH

Creating a tied pf::config::cached object

computeFromPath

Will load the Config::IniFiles object from cache or filesystem and update the cache

setControlFileTimestamp

Stores the current timestamp of var/cache_control file

getControlFileTimestamp

Get the current timestamp of var/cache_control file

controlFileExpired

Checks to see if the var/cache_control file has been updated

cache

Get the global CHI object for configfiles

cacheForData

Get the global CHI object for configfilesdata

ReloadConfigs

ReloadConfigs reload all configs and call any register callbacks

addReloadCallbacks

$self->addReloadCallbacks('name' => sub {...}); Add named callbacks to the onreload array Called in insert order If callback already exists, previous callback is replaced and previous position is preserved

_addCallbacks

Internal helper method for adding callbacks

addFileReloadCallbacks

$self->addFileReloadCallbacks('name' => sub {...}); Add named callbacks to the onfilereload array Called in insert order If callback already exists, previous callback is replaced and previous position is preserved

addFileReloadOnceCallbacks

$self->addFileReloadOnceCallbacks('name' => sub {...}); Add named callbacks to the onfilereloadonce array Called in insert order If callback already exists, previous callback is replaced and previous position is preserved

addCacheReloadCallbacks

$self->addCacheReloadCallbacks('name' => sub {...}); Add named callbacks to the onfilereload array Called in insert order If callback already exists, previous callback is replaced and previous position is preserved

addPostReloadCallbacks

$self->addPostReloadCallbacks('name' => sub {...}); Add named callbacks to the onpostreload array Called in insert order If callback already exists, previous callback is replaced and previous position is preserved

DESTROY

Cleaning up externally stored

addToLoadedConfigs

adds the cached config from the internal global cache

unloadConfig

Unloads the cached config from the internal global cache

toHash

Copy configuration to a hash

cleanupWhitespace

Clean up whitespace is a utility function for cleaning up whitespaces for hashes

AUTHOR

Inverse inc. <info@inverse.ca>

COPYRIGHT

Copyright (C) 2005-2015 Inverse inc.

LICENSE

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

<<