PHP dfw2 便利ツール - dfwLib2

dfw2 便利ツール

KVS ライク DB Class

  • KvsLikeDb.class.php

※ 要 DB Class required

<?php
class KvsLikeDb
{
   private $tableName = 'kvs_contents';
   
   public function __construct($tableName = null)
   {
       if (!empty($tableName)) $this->tableName = $tableName;
   }
   
   public function set($key, $val)
   {
       $item = $this->get($key);
       
       $db = DB::init();
       if (! empty($item)) {
           $db->setSql('UPDATE '.$this->tableName.' SET val = :val, updated_at = NOW() WHERE `key` = :key');
       } else {
           $db->setSql('INSERT INTO '.$this->tableName.' (`key`, val, updated_at) VALUES (:key, :val, NOW())');
       }
       $db->set('key', $key);
       $db->set('val', $val);
       
       return $db->exec();
   }
   
   public function get($key)
   {
       $db = DB::init();
       $db->setSql('SELECT * FROM '.$this->tableName.' WHERE `key` = :key');
       $db->set('key', $key);
       $item = $db->execFetchOne();
       if (empty($item)) return null;

       return $item['val'];
   }
} // End of Class
  • CREATE TABLE
CREATE  TABLE IF NOT EXISTS `kvs_contents` (
 `key` VARCHAR(64) NOT NULL ,
 `val` TEXT ,
 `updated_at` DATETIME NOT NULL,
 PRIMARY KEY (`key`) )
ENGINE = InnoDB;
  • 使い方
$kvs = new KvsLikeDb('my_kvs_contents');

$hash = $kvs->get('HASH');
echo $hash;

$kvs->set('HASH', sha1($json));

Memcache Class

  • dfwMemcached.class.php
<?php
/**
 * dfw Memchached class
 *
 * @see http://jp2.php.net/manual/ja/memcached.get.php
 */

if (!class_exists('dfwMemcached')) {
class dfwMemcached extends Memcached
{
   private $_connect = '';
   
   // construct
   public function __construct($connectName = 'default')
   {
       $this->_connect = $connectName;
       parent::__construct($connectName);
       $this->_init($connectName);
   }
   
   private function _init($connectName = 'default')
   {
       $memConfigs = $GLOBALS['dfw']['memcache'][$connectName];
       
       // Match HostName DSN
       if (!empty($memConfigs['hosts_match'])) {
           foreach (array_keys($memConfigs['hosts_match']) as $key) {
               if (empty($_SERVER['HTTP_HOST'])) break;
               if (strpos($_SERVER['HTTP_HOST'], $key) !== false) {
                   if (isset($memConfigs['hosts_match'][$key]['dsn'])) $memConfigs['dsn'] = $memConfigs['hosts_match'][$key]['dsn'];
                   break;
               }
           }
       }
       // Match ServerName DSN
       if (!empty($memConfigs['names_match'])) {
           foreach (array_keys($memConfigs['names_match']) as $key) {
               if (strpos(php_uname('n'), $key) !== false) {
                   if (isset($memConfigs['names_match'][$key]['dsn'])) $memConfigs['dsn'] = $memConfigs['names_match'][$key]['dsn'];
                   break;
               }
           }
       }
       
       $servers = explode(',', $memConfigs['dsn']);
       foreach ($servers as $server) {
           $set = explode(':', $server);
           if (empty($set[1])) $set[1] = '11211';
           $this->addServer(trim($set[0]), trim($set[1]));
       }
   }
   
} // End of Class
} // End of if(!class_exists('dfwMemcached'))
  • 設定(bootstrap.php 記載推奨)
$GLOBALS['dfw']['memcache']['default'] = array(
   'dsn' => '88.88.88.88:11216,88.88.88.77:11216',
   'names_match' => array(
       '-dev' => array('dsn' => '88.88.88.66:11216'),
   ),
   'hosts_match' => array(
       '-dev' => array('dsn' => '88.88.88.66:11216'),
   ),
);
  • 使い方
$mem = new dfwMemcached();

$name = $mem->get('name');
echo $name;

$mem->set('name', 'hogehoge');

バッチプログラムで writeLog

// Init PEAR Log
function initLog($filePath, $logLevel = 'info', $appName = '-')
{
   require_once 'Log.php';  // PEAR:Log
   
   $GLOBALS['dfw']['log']['core'] = Log::factory('file', $filePath, $appName,
           array('mode' => 0666, 'timeFormat' => '%Y/%m/%d %H:%M:%S', 'dirmode' => 0755)
       );
   $GLOBALS['dfw']['log']['level'] = $logLevel;
}

// write Log
function writeLog($message, $mode = 'info')
{
   if (empty($GLOBALS['dfw']['log']['core'])) return false;
   if (empty($GLOBALS['dfw']['log']['level'])) return false;

   $message = str_replace(array("\r\n", "\n"), ' ', $message);

   if ($mode === 'c' || $mode === 'critical') $GLOBALS['dfw']['log']['core']->crit($message);
   if ($GLOBALS['dfw']['log']['level'] === 'critical') return true;

   if ($mode === 'e' || $mode === 'error') $GLOBALS['dfw']['log']['core']->err($message);
   if ($GLOBALS['dfw']['log']['level'] === 'error') return true;

   if ($mode === 'w' || $mode === 'warning') $GLOBALS['dfw']['log']['core']->warning($message);
   if ($GLOBALS['dfw']['log']['level'] === 'warning') return true;

   if ($mode === 'i' || $mode === 'info') $GLOBALS['dfw']['log']['core']->info($message);
   if ($GLOBALS['dfw']['log']['level'] === 'info') return true;

   if ($mode === 'd' || $mode === 'debug') $GLOBALS['dfw']['log']['core']->debug($message);
   return true;
}