Impressum · Kontakt · Hilfe
Besucher online · Mitglieder



Portal > Foren > PHP > PHP-Programmierung > Caching mittels dbm-Dateien, Probleme mit dba_popen()

Layoutprobleme? - Styleswitcher!

Antwort
 
Themen-Optionen
Alt 27.07.2006, 04:18 Nach oben    #1
Ben
Benjamin Klaile
 
Benutzerbild von Ben
 
Registriert seit: 02.12.2004
Ort: Remagen
Beiträge: 3.812
Standard Caching mittels dbm-Dateien, Probleme mit dba_popen()

Hallo,
ich habe gerade mal etwas mit DBA rumgespielt. Heißt also: In diesem Fall geht es um Caching!

Nun gut. Ich habe jetzt also mal etwas gespielt und mir folgende Codes zusammengetippert.
PHP-Code:
<?php

class Cache_DBM
{
    
/**
     * The resource handler that provides access to the caching file.
     * 
     * @var mixed
     * A handler if the connection could be established. Otherwise 
     * <em>false</em>.
     * 
     * @access private
     */
    
private $dbmHandler;
    
    
/**
     * After the expiration (in seconds) the cache is actualized.
     * 
     * @var int 
     * The expiration in seconds.
     * 
     * @access private
     */
    
private $expiration;
    
    
    
/**
     * Creates an instance of class Cache_DBM and establishes a connection to
     * a dbm file. 
     * 
     * @param string $filename
     * The name of the caching file that is created.
     * 
     * @param int $expiration
     * The expiration time of the caching in seconds.
     * Default value is 30.
     * 
     * @access public
     */
    
public function __construct($filename$expiration 30)
    {
        
$this->dbmHandler dba_popen($filename"c""db3");
        
$this->expiration $expiration;
    }
    
    
    
/**
     * Puts data in association with a specified key into a dbm file 
     * respectively the cache.
     * 
     * 
     * @param string $keyName
     * The name of the key the data is associated with.
     * 
     * @param string $dataToStore
     * The data that should be cached.
     * 
     * @return void
     * @access public
     */
    
public function put($keyName$dataToStore)
    {
        
$storageData = array('object' => $dataToStore'time' => time());
        
dba_replace($keyNameserialize($storageData), $this->dbmHandler);
    }
    
    
    
/**
     * Gets the cached data according to the key name that is given.
     * If the cached data is too old it is deleted.
     * 
     * @param string $keyName
     * Name of the key the data is associated with.
     * 
     * @return mixed
     * If the cached data exists and is not too old the data is returned, 
     * else <em>false</em> is returned.
     * 
     * @access public
     */
    
public function get($keyName)
    {
        
// check if cached data exists
        
if(!$fetchedData dba_fetch($keyName$this->dbmHandler))
        {
            return 
false;
        }
        
        
/* 
         * unserialize the fetched data to receive the real data that was 
         * cached.
         */
        
$cachedData unserialize($fetchedData);
        
        
/*
         * check if the cached data has to be actualized. If not the cached 
         * data is returned, else the key data association is deleted.
         */
        
if(time() - $cachedData['time'] < $this->expiration)
        {
            return 
$cachedData;
        }
        
        
$this->delete($keyName); 
        return 
false;       
    }
    
    
    
/**
     * Deletes a key data association (cached data) out of a dbm file.
     * 
     * @param string $keyName
     * Name of the key the data is associated with.
     * 
     * @return void
     * @access public
     */
    
public function delete($keyName)
    {
        
dba_delete($keyName$this->dbmHandler);
    }
    
}

?>
PHP-Code:
<?php

class DummyClass
{
    private 
$output '';
    
    public function 
__construct()
    {
        
$this->output 'hello, this is a sample output.';
    }
    
    public function 
printOutput()
    {
        echo 
$this->output;
    }
}

?>
PHP-Code:
<?php

require_once('Cache_DBM.php');
require_once(
'DummyClass.php');


class 
StartApp
{
    
/**
     * Data that is used to gerenate the output.
     * 
     * @var mixed
     * @access private
     */
    
private $data null;
    
    
    
/**
     * Creates an instance of class StartApp and initializes the application. 
     */
    
public function __construct()
    {
        
$this->loadData();
        
$this->printData();
    }
    
    
/**
     * Loads data out of the cache or it is generated on the fly.
     * (It is just an example.)
     * 
     * @return void
     * @access private
     */
    
private function loadData()
    {
        
$filename $_SERVER['DOCUMENT_ROOT'] . '/dev/cachetest.db';
        
$cache = new Cache_DBM($filename);
        
        if(
$cachedData $cache->get('foo'))
        {
            
$this->data $cachedData;
            return;
        }        
        
        
$obj = new DummyClass();
        
ob_start();
        
$obj->printOutput();
        
$this->data ob_end_clean();        
    }
    
    
/**
     * Prints the cached respectively created output.
     * 
     * @return void
     * @access public
     */
    
public function printData()
    {
        echo 
$this->data;
    }
}


// Sample calling
$app = new StartApp(); 


?>
Der Quellcode sollte eigentlich mit Hilfe des Manuals selbsterklärend sein. Hoffe ich jedenfalls.

Nun gut. Ich erhalte nun beim Aufruf folgende Fehlermeldung:
Zitat:
Warning: dba_popen(C:/apachefriends/xampp/htdocs/dev/cachetest.db,c) [function.dba-popen]: Driver initialization failed for handler: db3: Permission denied in C:\[..]\Cache_DBM.php on line 42
Das ist folgende Zeile
PHP-Code:
$this->dbmHandler dba_popen($filename"c""db3"); 
Meine phpinfo()-Ausgabe diesbzgl. sieht so aus
Zitat:
dba
DBA support enabled
Supported handlers cdb cdb_make db3 inifile flatfile
Über Google bin ich auf http://bugs.php.net/bug.php?id=28122 gestoßen. Allerdings sollte das ja dann in PHP 5.1.2 (XAMPP 1.5.2) ebenfalls gefixed sein!

Ich bin verwirrt. Ich nutze Windows XP, aber das sollte doch trotzdem klappen oder?
Habe ich einen Fehler in der Anwendung oder woran kann es liegen?

Danke Euch für Eure Hilfe.
Grüße, Ben.


[EDIT]
Die Frage ist ja .. hat mein Verzeichnis die nötigen Rechte, um dort eine Datei zu erstellen?
Antwort: Ja, das hier klappt einwandfrei.
PHP-Code:
$x fopen("test.txt"'a'); 

[EDIT]
Also, die db-Datei wird erstellt, allerdings erhalte ich bei diesem Testcode hier
PHP-Code:
    public function __construct($filename$expiration 30)
    {
        
$this->dbmHandler dba_popen($filename"c""db3");
        if(!
$this->dbmHandler
        { 
            echo 
'Probleme mit dba_open()!';
        }
        
$this->expiration $expiration;
    } 
die Ausgabe "'Probleme mit dba_open()!" nach der Fehlermeldung.

*kopfkratz*
Raff ich nicht.

Geändert von Ben (27.07.2006 um 04:25 Uhr).
Ben ist offline  
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Alt 27.07.2006, 09:14 Nach oben    #2
björn
Benutzer
 
Registriert seit: 31.12.2005
Beiträge: 90
Standard

Muss es unbedingt popen sein()?
Versuche doch malk dba_open() !?
björn ist offline  
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Alt 27.07.2006, 11:52 Nach oben    #3
JaGeK
 
Beiträge: n/a
Standard

Zitat:
Zitat von Ben
Ich bin verwirrt. Ich nutze Windows XP, aber das sollte doch trotzdem klappen oder?
Habe ich einen Fehler in der Anwendung oder woran kann es liegen?
Ja, bei mir kommt es zu gleichem Fehler. Schau Dir einmal diesen Kommentar im Manual an - hat den Fehler hier verschwinden lassen.
 
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Alt 27.07.2006, 13:07 Nach oben    #4
Lars
me pro ok?
 
Benutzerbild von Lars
 
Registriert seit: 07.09.2005
Ort: Pulheim bei Köln
Beiträge: 964
Standard

dba_popen() ist in diesem Zusammenhand (Caching) durchaus sinnvoll.
__________________
Gedanken aus Draht stricken einen Zaun.
Lars ist offline  
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Alt 27.07.2006, 13:23 Nach oben    #5
Ben
Benjamin Klaile
 
Benutzerbild von Ben
 
Registriert seit: 02.12.2004
Ort: Remagen
Beiträge: 3.812
Standard

Zitat:
Zitat von björn
Versuche doch malk dba_open() !?
Macht keinerlei Unterschied. Hatte ich gestern schon ausgetestet.

Yeah. Es klappt nun mit
PHP-Code:
$this->dbmHandler dba_popen($filename"clt""db3"); 
Warum habe ich das übersehen? Egal.

Hatte noch einige weitere kleinen Denk- und Programmierfehler drin.
Mit diesen beiden Codes bin ich jetzt eigentlich ganz zufrieden (erstmal).
PHP-Code:
<?php

class Cache_DBM
{
    
/**
     * The resource handler that provides access to the caching file.
     * 
     * @var mixed
     * A handler if the connection could be established. Otherwise 
     * <em>false</em>.
     * 
     * @access private
     */
    
private $dbmHandler;
    
    
/**
     * After the expiration (in seconds) the cache is actualized.
     * 
     * @var int 
     * The expiration in seconds.
     * 
     * @access private
     */
    
private $expiration;
    
    
    
/**
     * Creates an instance of class Cache_DBM and establishes a connection to
     * a dbm file. 
     * 
     * @param string $filename
     * The name of the caching file that is created.
     * 
     * @param int $expiration
     * The expiration time of the caching in seconds.
     * Default value is 30.
     * 
     * @access public
     */
    
public function __construct($filename$expiration 30)
    {
        
$this->dbmHandler dba_popen($filename"clt""db3");
        
$this->expiration $expiration;
    }
    
    
    
/**
     * Puts data in association with a specified key into a dbm file 
     * respectively the cache.
     * 
     * 
     * @param string $keyName
     * The name of the key the data is associated with.
     * 
     * @param string $dataToStore
     * The data that should be cached.
     * 
     * @return void
     * @access public
     */
    
public function put($keyName$dataToStore)
    {
        
$storageData = array('object' => $dataToStore'time' => time());
        
dba_replace($keyNameserialize($storageData), $this->dbmHandler);
    }
    
    
    
/**
     * Gets the cached data according to the key name that is given.
     * If the cached data is too old it is deleted.
     * 
     * @param string $keyName
     * Name of the key the data is associated with.
     * 
     * @return mixed
     * If the cached data exists and is not too old the data is returned, 
     * else <em>false</em> is returned.
     * 
     * @access public
     */
    
public function get($keyName)
    {
        
// check if cached data exists
        
if(!$fetchedData dba_fetch($keyName$this->dbmHandler))
        {
            return 
false;
        }
        
        
/* 
         * unserialize the fetched data to receive the real data that was 
         * cached.
         */
        
$cachedData unserialize($fetchedData);
        
        
/*
         * check if the cached data has to be actualized. If not the cached 
         * data is returned, else the key data association is deleted.
         */
        
if(time() - $cachedData['time'] < $this->expiration)
        {
            return 
$cachedData;
        }
        
        
$this->delete($keyName); 
        return 
false;       
    }
    
    
    
/**
     * Deletes a key data association (cached data) out of a dbm file.
     * 
     * @param string $keyName
     * Name of the key the data is associated with.
     * 
     * @return void
     * @access public
     */
    
public function delete($keyName)
    {
        
dba_delete($keyName$this->dbmHandler);
    }
    
}

?>
PHP-Code:
<?php

require_once('Cache_DBM.php');
require_once(
'DummyClass.php');


class 
StartApp
{
    
/**
     * Data that is used to gerenate the output.
     * 
     * @var mixed
     * @access private
     */
    
private $dataObject null;
    
    
    
/**
     * Creates an instance of class StartApp and initializes the application. 
     */
    
public function __construct()
    {
        
$this->loadData();
        
$this->printData();
    }
    
    
/**
     * Loads data out of the cache or it is generated on the fly.
     * (It is just an example.)
     * 
     * @return void
     * @access private
     */
    
private function loadData()
    {
        
$filename $_SERVER['DOCUMENT_ROOT'] . '/dev/cachetest.db';
        
$cache = new Cache_DBM($filename);
        
        if(
$cachedData $cache->get('sampleDataObject'))
        {            
            
$this->dataObject $cachedData['object'];
            return;
        }        
        
        
$obj = new DummyClass();
        
// cache data object
        
$cache->put('sampleDataObject'$obj);    
        
$this->dataObject $obj;  
    }
    
    
/**
     * Prints the cached respectively created output.
     * 
     * @return void
     * @access public
     */
    
public function printData()
    {        
        echo 
$this->dataObject->printOutput();
    }
}


// Sample calling
$app = new StartApp(); 


?>
Die Frage ist jetzt nur. Ich speichere hier ein Objekt in der .db-Datei. Was ist aber, wenn ich dort kein Objekt, sondern z.B. einen String speichere. Dann müsste ich ja den Zugriff auf das die Variable $cachedData abändern, da ja kein Objekt mehr existiert.

Hat da eventuell noch jemand eine Idee / einen Workaround.

Grüße, Ben.
Ben ist offline  
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Antwort

« HTML ausgeben | Wie kann ich eine Schleife in einem Template ausleses? »

Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 
Themen-Optionen

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge anzufügen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

vB Code ist An.
Smileys sind An.
[IMG] Code ist An.
HTML-Code ist Aus.
Trackbacks are An
Pingbacks are An
Refbacks are Aus

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
GZip-Komprimierung mehrerer Dateien mittels PHP Chr!s PHP-Programmierung 4 07.04.2007 14:10
Caching mittels HTTP-Code 404 Byrel Tools, Server, Betriebssysteme 37 27.12.2006 10:12
[PHP] Simples Caching System mittels Dateien Chr!s Tutorials 5 05.11.2006 00:55


Alle Zeitangaben in WEZ +2. Es ist jetzt 08:15 Uhr.

Nach oben
Wir nutzen das Zend Framework, vBulletin (vBulletin v3.6.7, Copyright ©2000-2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0) und vBSEO.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44