Ver Fonte

Move caching to separate cache and create basic implementation

Barry O'Donovan há 13 anos atrás
pai
commit
c625cba998
3 ficheiros alterados com 210 adições e 15 exclusões
  1. 58 0
      OSS/Cache.php
  2. 110 0
      OSS/Cache/Basic.php
  3. 42 15
      OSS/SNMP.php

+ 58 - 0
OSS/Cache.php

@@ -0,0 +1,58 @@
+<?php
+
+/*
+    Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
+    All rights reserved.
+
+    Contact: Barry O'Donovan - barry (at) opensolutions (dot) ie
+             http://www.opensolutions.ie/
+
+    This file is part of the OSS_SNMP package.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+
+        * Redistributions of source code must retain the above copyright
+          notice, this list of conditions and the following disclaimer.
+        * Redistributions in binary form must reproduce the above copyright
+          notice, this list of conditions and the following disclaimer in the
+          documentation and/or other materials provided with the distribution.
+        * Neither the name of Open Source Solutions Limited nor the
+          names of its contributors may be used to endorse or promote products
+          derived from this software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+namespace OSS;
+
+
+/**
+ * An abstract cache for storing results of SNMP queries .
+ *
+ * See the implementation in \OSS\Cache\Basic for proper examples and documentation.
+ *
+ * @see \OSS\Cache\Basic
+ * @copyright Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
+ * @author Barry O'Donovan <barry@opensolutions.ie>
+ */
+abstract class Cache
+{
+
+    abstract protected function save( $varName, $varValue );
+    abstract protected function load( $varName );
+    abstract protected function clear( $varName );
+    abstract protected function clearAll();
+
+}
+
+

+ 110 - 0
OSS/Cache/Basic.php

@@ -0,0 +1,110 @@
+<?php
+
+/*
+    Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
+    All rights reserved.
+
+    Contact: Barry O'Donovan - barry (at) opensolutions (dot) ie
+             http://www.opensolutions.ie/
+
+    This file is part of the OSS_SNMP package.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+
+        * Redistributions of source code must retain the above copyright
+          notice, this list of conditions and the following disclaimer.
+        * Redistributions in binary form must reproduce the above copyright
+          notice, this list of conditions and the following disclaimer in the
+          documentation and/or other materials provided with the distribution.
+        * Neither the name of Open Source Solutions Limited nor the
+          names of its contributors may be used to endorse or promote products
+          derived from this software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+namespace OSS\Cache;
+
+/**
+ * basic (array) cache implementation
+ *
+ * @copyright Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
+ * @author Barry O'Donovan <barry@opensolutions.ie>
+ */
+class Basic extends \OSS\Cache
+{
+
+    /**
+     * An array to store results - a temporary cache
+     * @var array An array to store results - a temporary cache
+     */
+    protected $_cache;
+
+
+    public function __construct()
+    {
+        $_cache = array();
+    }
+
+
+
+    /**
+     * Load a named value from the cache (or null if not present)
+     *
+     * @param string $var The name of the value to load
+     * @return mixed|null The value from the cache or null
+     */
+    public function load( $var )
+    {
+        if( isset( $this->_cache[ $var ] ) )
+            return $this->_cache[ $var ];
+
+        return null;
+    }
+
+
+    /**
+     * Save a named value to the cache
+     *
+     * @param string $var The name of the value to save
+     * @param mixed  $val The value to save
+     * @return mixed The value (as passed)
+     */
+    public function save( $var, $val )
+    {
+        return $this->_cache[ $var ] = $val;
+    }
+
+    /**
+     * Clear a named value from the cache
+     *
+     * @param string $var The name of the value to clear
+     */
+    public function clear( $va )
+    {
+        if( isset( $this->_cache[ $var ] ) )
+            unset( $this->_cache[ $var ] );
+    }
+
+
+    /**
+     * Clear all values from the cache
+     *
+     */
+    public function clearAll()
+    {
+        $this->_cache = array();
+    }
+
+}
+

+ 42 - 15
OSS/SNMP.php

@@ -81,12 +81,11 @@ class SNMP
      */
     protected $_lastResult = null;
 
-
     /**
-     * An array to store processed results - a temporary cache
-     * @var array An array to store processed results - a temporary cache
+     * The cache object to use as the cache
+     * @var \OSS\Cache The cache object to use
      */
-    protected $_resultCache;
+    protected $_cache = null;
 
     /**
      * Set to true to disable local cache lookup and force SNMP queries
@@ -136,8 +135,6 @@ class SNMP
      */
     public function __construct( $host = '127.0.0.1', $community = 'public' )
     {
-        $this->_resultCache = array();
-
         return $this->setHost( $host )
                     ->setCommunity( $community )
                     ->setOidOutputFormat( self::OID_OUTPUT_NUMERIC );
@@ -164,12 +161,12 @@ class SNMP
      */
     public function get( $oid )
     {
-        if( $this->cache() && isset( $this->_resultCache[$oid] ) )
-            return $this->_resultCache[$oid];
+        if( $this->cache() && ( $rtn = $this->getCache()->load( $oid ) ) !== null )
+            return $rtn;
 
         $this->_lastResult = snmp2_get( $this->getHost(), $this->getCommunity(), $oid, $this->getTimeout(), $this->getRetry() );
 
-        return $this->_resultCache[$oid] = $this->parseSnmpValue( $this->_lastResult );
+        return $this->getCache()->save( $oid, $this->parseSnmpValue( $this->_lastResult ) );
     }
 
     /**
@@ -199,8 +196,8 @@ class SNMP
      */
     public function walk1d( $oid )
     {
-        if( $this->cache() && isset( $this->_resultCache[$oid] ) )
-            return $this->_resultCache[$oid];
+        if( $this->cache() && ( $rtn = $this->getCache()->load( $oid ) ) !== null )
+            return $rtn;
 
         $this->_lastResult = snmp2_real_walk( $this->getHost(), $this->getCommunity(), $oid, $this->getTimeout(), $this->getRetry() );
 
@@ -217,7 +214,7 @@ class SNMP
             $result[ substr( $_oid, strrpos( $_oid, '.' ) + 1 ) ] = $this->parseSnmpValue( $value );
         }
 
-        return $this->_resultCache[$oid] = $result;
+        return $this->getCache()->save( $oid, $result );
     }
 
     /**
@@ -244,8 +241,8 @@ class SNMP
      */
     public function subOidWalk( $oid, $position )
     {
-        if( $this->cache() && isset( $this->_resultCache[$oid] ) )
-            return $this->_resultCache[$oid];
+        if( $this->cache() && ( $rtn = $this->getCache()->load( $oid ) ) !== null )
+            return $rtn;
 
         $this->_lastResult = snmp2_real_walk( $this->getHost(), $this->getCommunity(), $oid, $this->getTimeout(), $this->getRetry() );
 
@@ -258,7 +255,7 @@ class SNMP
             $result[ $oids[ $position] ] = $this->parseSnmpValue( $value );
         }
 
-        return $this->_resultCache[$oid] = $result;
+        return $this->getCache()->save( $oid, $result );
     }
 
 
@@ -510,6 +507,36 @@ class SNMP
         return !$this->_disableCache;
     }
 
+    /**
+     * Set the cache to use
+     *
+     * @param \OSS\Cache $c The cache to use
+     * @return \OSS\SNMP For fluent interfaces
+     */
+    public function setCache( $c )
+    {
+        $this->_cache = $c;
+        return $this;
+    }
+
+    /**
+     * Get the cache in use (or create a Cache\Basic instance
+     *
+     * We kind of mandate the use of a cache as the code is written with a cache in mind.
+     * You are free to disable it via disableCache() but your machines may be hammered!
+     *
+     * We would suggest disableCache() / enableCache() used in pairs only when really needed.
+     *
+     * @return \OSS\Cache The cache object
+     */
+    public function getCache()
+    {
+        if( $this->_cache === null )
+            $this->_cache = new \OSS\Cache\Basic();
+
+        return $this->_cache;
+    }
+
 
     /**
      * Magic method for generic function calls