ALT Linux Team development discussions
 help / color / mirror / Atom feed
From: Alexey Tourbin <at@altlinux.ru>
To: ALT Devel discussion list <devel@altlinux.org>
Subject: [devel] cmdcache
Date: Mon, 12 Sep 2005 15:35:51 +0400
Message-ID: <20050912113551.GI3290@solemn.turbinal.org> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 763 bytes --]

On Sun, Sep 11, 2005 at 09:06:44PM +0400, Alexey Tourbin wrote:
> Написал на шелле, с кешированием для каталогов.  Для робота это важно --
> очень долго (с распаковкой всех rpm'ов) только один раз будет работать.

По поводу кеширования мне пришла в голову ещё одна мысль.
Некоторые команды, которые работают долго, и вывод которых стоит
кешировать, имеют вид

	cmd [args...] path

В случае, если состояние сущности path определяется датой модификации
path, то команду можно завернуть в скрипт или функцию, которая перед
запуском команды проверят наличие результата в кеше:

	cmdcache cmd [args...] path

Вот предварительная реализация этой идеи.  Почему-то очень много кода
получилось. :(  Если кто-то знает, как сделать лучше, подскажите плиз.

[-- Attachment #1.2: cmdcache --]
[-- Type: text/plain, Size: 1807 bytes --]

#!/bin/sh -ef

cmdcache()
{
	if [ $# -lt 2 ]; then
		echo "cmdcache: not enough arguments" >&2
		pod2usage --exit=2 cmdcache || [ $? = 2 ]
		return 2
	fi

	local cmd="$1"; shift
	local cdir="$HOME/.cmdcache/${cmd##*/}"
	[ -d "$cdir" ] || mkdir -p -m700 "$cdir" || return 1
	[ -n "${RANDOM##*0}" ] || find "$cdir" -type f -mtime +33 -atime +33 -delete

	local file; for file; do :; done
	if [ ! -e "$file" ]; then
		echo "cmdcache: $file: no such file or directory" >&2
		return 1
	fi

	local cache="$*"; cache="$cdir/${cache//[-.\/ ]/_}"
	if [ -f "$cache" ] && ! [ "$file" -nt "$cache" -o "$cache" -nt "$file" ]; then
		cat "$cache"
		return
	fi

	local rc=0
	local mtime0; mtime0="$(stat --format $'%Y\n' "$file")" || return

	"$cmd" "$@" >"$cache" && touch -r "$file" "$cache" || rc=$?
	cat "$cache"
	
	local mtime1; mtime1="$(stat --format $'%Y\n' "$file")" || rc=$?
	if [ $rc = 0 -a "$mtime0" != "$mtime1" ]; then
		echo "cmdcache: $file has changed" >&2
		rc=1
	fi

	[ $rc = 0 ] || rm -f "$cache"
	return $rc
}

if [ -z "$*" -a "${0##*/}" != cmdcache ]; then
	: okay, maybe I am sourced
elif [ "x$*" = x-h -o "x$*" = x--help ]; then
	pod2usage --exit=0 "$0"
else 
	cmdcache "$@"
fi

: <<'__EOF__'

=head1	NAME

cmdcache - simple command cache based on timestamps

=head1	SYNOPSIS

B<cmdcache> I<cmd> [I<args>...] I<FILE>

B<source> B<cmdcache> ''

=head1	AUTHOR

Written by Alexey Tourbin <at@altlinux.org>.

=head1	COPYING

Copyright (c) 2005 Alexey Tourbin, ALT Linux Team.

This 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.

=cut

__EOF__

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

             reply	other threads:[~2005-09-12 11:35 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-12 11:35 Alexey Tourbin [this message]
2005-09-12 13:05 ` [devel] cmdcache Alexey Tourbin
2005-09-12 13:34   ` Dmitry V. Levin
2005-09-12 14:01     ` Alexey Tourbin
2005-09-12 14:57     ` [devel] [JT] " Michael Shigorin
2005-09-12 18:35       ` [devel] " Alexey Tourbin
2005-09-13  5:21     ` [devel] " Alexey Tourbin
2005-09-14 15:35       ` Alexey Tourbin
2005-09-14 15:47         ` Michael Shigorin
2005-09-14 16:12           ` Alexey Tourbin
2005-09-14 16:38             ` Michael Shigorin
2005-09-14 17:10               ` Alexey Tourbin
2005-09-15  9:26                 ` [devel] Re: cmdcache hash function Alexey Tourbin
2005-09-15 14:37                   ` Alexey Tourbin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050912113551.GI3290@solemn.turbinal.org \
    --to=at@altlinux.ru \
    --cc=devel@altlinux.org \
    --cc=devel@altlinux.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux Team development discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
		devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
	public-inbox-index devel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git