#!/usr/bin/perl
# -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# cleancache: 	remove entries from the cache that are not used recently
# (c) 2001 Ask Solem Hoel <ask@unixmonks.net>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2,
#   *NOT* "earlier versions", as published by the Free Software Foundation.
#
#   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#####

use strict;
use vars qw($CACHE_DB $CACHE_TIMESTAMP_DB $TIME_TO_DELETE);
use DB_File;

#%ifdef CACHE_DB
#%print $CACHE_DB = '%{CACHE_DB}';
#%else
$CACHE_DB = 'xiri-cache.db';
#%endif

#%ifdef CACHE_TIMESTAMP_DB
#%print $CACHE_TIMESTAMP_DB = '%{CACHE_TIMESTAMP_DB}';
#%else
$CACHE_TIMESTAMP_DB = 'xiri-cache-timestamp.db';
#%endif

$TIME_TO_DELETE = 5 * 24 * 60 * 60; # 5 days

# ### open the cache database
my $y = tie my %cache, 'DB_File', $CACHE_DB, O_CREAT|O_RDWR, 0644, $DB_HASH;
my $z = tie my %ctime, 'DB_File', $CACHE_TIMESTAMP_DB, O_CREAT|O_RDWR, 0644, $DB_HASH;

foreach my $search (keys %ctime) {
	print "Record: $search...\n";
	print $cache{$search}, "\n\n";
	if(localtime >= $ctime{$search} + $TIME_TO_DELETE) {
		print "Deleting record $search...\n";
		delete $ctime{$search};
		delete $cache{$search};
	}
}

$y->sync();
$z->sync();
