2006年09月14日

package mop.gamespace.dkp;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**parseobj class
 * @author Administrator
 *
 */
public class ParseRaidTracker {
 
 private String RaidTime;
 
 private ArrayList joinXml;

 private ArrayList leaveXml;

 private ArrayList lootXml;
 
 private String savefilename = "./temp.xml";

 public ParseRaidTracker() {

  RaidTime = new String();
  joinXml = new ArrayList();
  leaveXml = new ArrayList();
  lootXml = new ArrayList();
 }
 
 /**parseObjFromStr
  * @param str
  * @return ArrayList
  */
 public ArrayList parseObjFromStr(String str)
 {
  String checkstr;
  checkstr = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
  if(str.indexOf(checkstr)== -1)
   str = checkstr +str;
  File file = new File(savefilename);
  BufferedWriter bw = null;
  try {
   bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
   bw.write(str);
   bw.flush();
  } catch (FileNotFoundException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  } catch (IOException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  finally
  {
   try {
    if(bw != null)
    {
     bw.close();
    }
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
   
  }
  return parseObjFromFile(savefilename);
  
 }
 /**parseObjFromFile
  * @param filename
  * @return ArrayList
  */
 public ArrayList parseObjFromFile(String filename) {
  ArrayList returnAL = new ArrayList();
  try {
   SAXReader saxReader = new SAXReader();
   Document document = saxReader.read(new File(filename));
   Element ee = document.getRootElement();

   Iterator t;
   Element element;
   t = ee.elementIterator("key");
   element = (Element) t.next();
   RaidTime = element.getStringValue();
   t = ee.elementIterator("Join");
   element = (Element) t.next();
   joinXml = parsejoinElement(element);
   t= ee.elementIterator("Leave");
   element = (Element) t.next();
   leaveXml = parseleaveElement(element);
   t = ee.elementIterator("Loot");
   element = (Element) t.next();
   lootXml = parselootElement(element);
   
   returnAL.add(RaidTime);
   returnAL.add(joinXml);
   returnAL.add(leaveXml);
   returnAL.add(lootXml);

  } catch (DocumentException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  } finally {
   
  }
  return returnAL;

 }

 /**parsejoinElement
  * @param _element
  * @return ArrayList
  */
 public ArrayList parsejoinElement(Element _element)
 {
  ArrayList eleArr = new ArrayList();
  Element elementKey;
  Element element;
  JoinPlayer obj;
  JoinPlayer tmpobj;
  Iterator tmpIterator;
  boolean flg = true;
  int j = 0;
  for (Iterator i = _element.elementIterator(); i.hasNext();)
  {
   flg = true;
   elementKey = (Element) i.next();
   obj = new JoinPlayer();
   tmpIterator = elementKey.elementIterator("player");
   element = (Element) tmpIterator.next();
   obj.setplayer(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("race");
   element = (Element) tmpIterator.next();
   obj.setrace(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("class");
   element = (Element) tmpIterator.next();
   obj.setclass(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("level");
   element = (Element) tmpIterator.next();
   obj.setlevel(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("time");
   element = (Element) tmpIterator.next();
   obj.settime(element.getStringValue().trim().replace("\’","\\\’"));
   for(j=0;j<eleArr.size();j++)
   {
    tmpobj = new JoinPlayer();
    tmpobj = (JoinPlayer)eleArr.get(j);
    if(tmpobj.getplayer().equals(obj.getplayer()))
    {
     flg = false;
     break;
    }
   }
   if(flg)
    eleArr.add(obj);
  }
  return eleArr;

 }
 /**parseleaveElement
  * @param _element
  * @return ArrayList
  */
 public ArrayList parseleaveElement(Element _element)
 {
  ArrayList eleArr = new ArrayList();
  Element elementKey;
  Element element;
  LeavePlayer obj;
  Iterator tmpIterator;

  for (Iterator i = _element.elementIterator(); i.hasNext();)
  {
   elementKey = (Element) i.next();
   obj = new LeavePlayer();
   tmpIterator = elementKey.elementIterator("player");
   element = (Element) tmpIterator.next();
   obj.setplayer(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("time");
   element = (Element) tmpIterator.next();
   obj.settime(element.getStringValue().trim().replace("\’","\\\’"));

   eleArr.add(obj);
  }
  return eleArr;

 }
 /**parselootElement
  * @param _element
  * @return ArrayList
  */
 public ArrayList parselootElement(Element _element)
 {
  ArrayList eleArr = new ArrayList();
  Element elementKey;
  Element element;
  LootItem obj;
  Iterator tmpIterator;

  for (Iterator i = _element.elementIterator(); i.hasNext();)
  {
   elementKey = (Element) i.next();
   obj = new LootItem();
   tmpIterator = elementKey.elementIterator("ItemName");
   element = (Element) tmpIterator.next();
   obj.setitemname(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("ItemID");
   element = (Element) tmpIterator.next();
   obj.setitemid(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Color");
   element = (Element) tmpIterator.next();
   obj.setcolor(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Count");
   element = (Element) tmpIterator.next();
   obj.setcount(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Player");
   element = (Element) tmpIterator.next();
   obj.setplayer(element.getStringValue().trim().replace("\’","\\\’"));
   tmpIterator = elementKey.elementIterator("Time");
   element = (Element) tmpIterator.next();
   obj.settime(element.getStringValue().trim().replace("\’","\\\’"));
   
   eleArr.add(obj);
  }
  return eleArr;

 }

}

2006年08月17日

dom4j这东西是java的一个架包,由XXorg开发的,我也不知道…

写在前面…

Welcome to dom4j!

dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP.

dom4j 是一种使用简单,开源的使用了java框架收集技术和全面支持DOM,SAX和JAXP,工作在java平台的XML,XPath和XSLT的工具

。。。。以上为我翻译

Dom4j是一个易用的、开源的库,用于XMLXPathXSLT。它应用于Java平台,采用了Java集合框架并完全支持DOMSAXJAXP

。。。。以上为别人翻译
我使用了它来解析xml文件。
部分代码:
ee是一个Element

for ( Iterator i = ee.elementIterator(); i.hasNext(); ) {

          Element element = (Element) i.next();
          s1 = element.getName();
          if(s1.equals("Join"))
          {
           for(Iterator ii = element.elementIterator(); ii.hasNext();)
           {
            Element eee = (Element) ii.next();
            for(Iterator iii = eee.elementIterator(); iii.hasNext();)
            {
             Element e4 = (Element) iii.next();
             s3 = e4.getName();
             System.out.println(s3+"::"+e4.getStringValue());
            }
            s2 = eee.getName();
            System.out.println(s2);
           }
          }
          // do something
          System.out.println(s1);
          //System.out.println(element.getData().toString());
       }

2006年08月15日

Discuz!学习记

一个很好的机会,看了Discuz!的不少代码。

这个小项目说小也小,说大也大。

小:只需要改动几行代码而已。

大:需要修改数据库结构,而且还需要从别的数据库把数据导过来。

 

不过导数据没分到我头上,呵呵。

看到同事导那80w+的数据头疼阿,差不多导了8个小时。好多中文数据莫名其妙的不能使用,很奇怪。

Discuz!的代码是个学习的好榜样,不知道我什么时候才能写那么好的代码@@

但是我不清楚为什么它每次登录的时候都要去update  表members的全部数据一次。很奇怪。

 

2006年08月11日

.htaccess

关于这个文件的说明

ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/internalerror.html

 

2006年08月09日



com.danga.MemCached
Class MemCachedClient

java.lang.Object  extended by com.danga.MemCached.MemCachedClient 

public class MemCachedClient
extends java.lang.Object

This is a Java client for the memcached server available from http://www.danga.com/memcached/.
Supports setting, adding, replacing, deleting compressed/uncompressed and
serialized (can be stored as string if object is native class) objects to memcached.

Now pulls SockIO objects from SockIOPool, which is a connection pool. The server failover
has also been moved into the SockIOPool class.
This pool needs to be initialized prior to the client working. See javadocs from SockIOPool.

Some examples of use follow.

To create cache client object and set params:

        MemCachedClient mc = new MemCachedClient();

        // compression is enabled by default            mc.setCompressEnable(true);

        // set compression threshhold to 4 KB (default: 15 KB)          mc.setCompressThreshold(4096);

        // turn on storing primitive types as a string representation        // Should not do this in most cases.            mc.setPrimitiveAsString(true); 

To store an object:

        MemCachedClient mc = new MemCachedClient();        String key   = "cacheKey1";             Object value = SomeClass.getObject();           mc.set(key, value); 

To store an object using a custom server hashCode:

        MemCachedClient mc = new MemCachedClient();        String key   = "cacheKey1";             Object value = SomeClass.getObject();           Integer hash = new Integer(45);         mc.set(key, value, hash); 

The set method shown above will always set the object in the cache.
The add and replace methods do the same, but with a slight difference.

  • add — will store the object only if the server does not have an entry for this key
  • replace — will store the object only if the server already has an entry for this key

To delete a cache entry:

        MemCachedClient mc = new MemCachedClient();        String key   = "cacheKey1";             mc.delete(key); 

To delete a cache entry using a custom hash code:

        MemCachedClient mc = new MemCachedClient();        String key   = "cacheKey1";             Integer hash = new Integer(45);         mc.delete(key, hashCode); 

To store a counter and then increment or decrement that counter:

        MemCachedClient mc = new MemCachedClient();        String key   = "counterKey";            mc.storeCounter(key, new Integer(100));        System.out.println("counter after adding      1: " mc.incr(key));               System.out.println("counter after adding      5: " mc.incr(key, 5));            System.out.println("counter after subtracting 4: " mc.decr(key, 4));            System.out.println("counter after subtracting 1: " mc.decr(key));        

To store a counter and then increment or decrement that counter with custom hash:

        MemCachedClient mc = new MemCachedClient();        String key   = "counterKey";            Integer hash = new Integer(45);         mc.storeCounter(key, new Integer(100), hash);        System.out.println("counter after adding      1: " mc.incr(key, 1, hash));              System.out.println("counter after adding      5: " mc.incr(key, 5, hash));              System.out.println("counter after subtracting 4: " mc.decr(key, 4, hash));              System.out.println("counter after subtracting 1: " mc.decr(key, 1, hash));       

To retrieve an object from the cache:

        MemCachedClient mc = new MemCachedClient();        String key   = "key";           Object value = mc.get(key);      

To retrieve an object from the cache with custom hash:

        MemCachedClient mc = new MemCachedClient();        String key   = "key";           Integer hash = new Integer(45);         Object value = mc.get(key, hash);        

To retrieve an multiple objects from the cache

        MemCachedClient mc = new MemCachedClient();        String[] keys   = { "key", "key1", "key2" };        Object value = mc.getMulti(keys); 

To retrieve an multiple objects from the cache with custom hashing

        MemCachedClient mc = new MemCachedClient();        String[] keys    = { "key", "key1", "key2" };        Integer[] hashes = { new Integer(45), new Integer(32), new Integer(44) };        Object value = mc.getMulti(keys, hashes); 

To flush all items in server(s)

        MemCachedClient mc = new MemCachedClient();        mc.flushAll(); 

To get stats from server(s)

        MemCachedClient mc = new MemCachedClient();        Map stats = mc.stats(); 

Version:
1.3.2
Author:
greg whalin

Field Summary
private  java.lang.ClassLoader classLoader
           
private static java.lang.String CLIENT_ERROR
           
private static int COMPRESS_THRESH
           
private  boolean compressEnable
           
private  long compressThreshold
           
private  java.lang.String defaultEncoding
           
private static java.lang.String DELETED
           
private static java.lang.String END
           
private static java.lang.String ERROR
           
private static int F_COMPRESSED
           
private static int F_SERIALIZED
           
private static java.lang.String ITEM
           
private static Logger log
           
private static java.lang.String NOTFOUND
           
private static java.lang.String NOTSTORED
           
private static java.lang.String OK
           
private  java.lang.String poolName
           
private  boolean primitiveAsString
           
private static java.lang.String SERVER_ERROR
           
private static java.lang.String STATS
           
private static java.lang.String STORED
           
private static java.lang.String VALUE
           

 

Constructor Summary
MemCachedClient()
          Creates a new instance of MemCachedClient.
MemCachedClient(java.lang.ClassLoader classLoader)
          Creates a new instance of MemCacheClient but acceptes a passed in ClassLoader.

 

Method Summary
 boolean add(java.lang.String key, java.lang.Object value)
          Adds data to the server; only the key and the value are specified.
 boolean add(java.lang.String key, java.lang.Object value, java.util.Date expiry)
          Adds data to the server; the key, value, and an expiration time are specified.
 boolean add(java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode)
          Adds data to the server; the key, value, and an expiration time are specified.
 boolean add(java.lang.String key, java.lang.Object value, java.lang.Integer hashCode)
          Adds data to the server; the key, value, and an optional hashcode are passed in.
 long decr(java.lang.String key)
          Decrement the value at the specified key by 1, and then return it.
 long decr(java.lang.String key, long inc)
          Decrement the value at the specified key by passed in value, and then return it.
 long decr(java.lang.String key, long inc, java.lang.Integer hashCode)
          Decrement the value at the specified key by the specified increment, and then return it.
 boolean delete(java.lang.String key)
          Deletes an object from cache given cache key.
 boolean delete(java.lang.String key, java.util.Date expiry)
          Deletes an object from cache given cache key and expiration date.
 boolean delete(java.lang.String key, java.lang.Integer hashCode, java.util.Date expiry)
          Deletes an object from cache given cache key, a delete time, and an optional hashcode.
 boolean flushAll()
          Invalidates the entire cache.
 boolean flushAll(java.lang.String[] servers)
          Invalidates the entire cache.
 java.lang.Object get(java.lang.String key)
          Retrieve a key from the server, using a specific hash.
 java.lang.Object get(java.lang.String key, java.lang.Integer hashCode)
          Retrieve a key from the server, using a specific hash.
 java.lang.Object get(java.lang.String key, java.lang.Integer hashCode, boolean asString)
          Retrieve a key from the server, using a specific hash.
 long getCounter(java.lang.String key)
          Returns value in counter at given key as long.
 long getCounter(java.lang.String key, java.lang.Integer hashCode)
          Returns value in counter at given key as long.
 java.util.Map getMulti(java.lang.String[] keys)
          Retrieve multiple objects from the memcache.
 java.util.Map getMulti(java.lang.String[] keys, java.lang.Integer[] hashCodes)
          Retrieve multiple keys from the memcache.
 java.util.Map getMulti(java.lang.String[] keys, java.lang.Integer[] hashCodes, boolean asString)
          Retrieve multiple keys from the memcache.
 java.lang.Object[] getMultiArray(java.lang.String[] keys)
          Retrieve multiple objects from the memcache.
 java.lang.Object[] getMultiArray(java.lang.String[] keys, java.lang.Integer[] hashCodes)
          Retrieve multiple objects from the memcache.
 java.lang.Object[] getMultiArray(java.lang.String[] keys, java.lang.Integer[] hashCodes, boolean asString)
          Retrieve multiple objects from the memcache.
 long incr(java.lang.String key)
          Increment the value at the specified key by 1, and then return it.
 long incr(java.lang.String key, long inc)
          Increment the value at the specified key by passed in val.
 long incr(java.lang.String key, long inc, java.lang.Integer hashCode)
          Increment the value at the specified key by the specified increment, and then return it.
private  long incrdecr(java.lang.String cmdname, java.lang.String key, long inc, java.lang.Integer hashCode)
          Increments/decrements the value at the specified key by inc.
private  void init()
          Initializes client object to defaults.
 boolean keyExists(java.lang.String key)
          Checks to see if key exists in cache.
private  void loadItems(SockIOPool.SockIO sock, java.util.Map hm, boolean asString)
          This method loads the data from cache into a Map.
 boolean replace(java.lang.String key, java.lang.Object value)
          Updates data on the server; only the key and the value are specified.
 boolean replace(java.lang.String key, java.lang.Object value, java.util.Date expiry)
          Updates data on the server; the key, value, and an expiration time are specified.
 boolean replace(java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode)
          Updates data on the server; the key, value, and an expiration time are specified.
 boolean replace(java.lang.String key, java.lang.Object value, java.lang.Integer hashCode)
          Updates data on the server; only the key and the value and an optional hash are specified.
 boolean set(java.lang.String key, java.lang.Object value)
          Stores data on the server; only the key and the value are specified.
 boolean set(java.lang.String key, java.lang.Object value, java.util.Date expiry)
          Stores data on the server; the key, value, and an expiration time are specified.
 boolean set(java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode)
          Stores data on the server; the key, value, and an expiration time are specified.
 boolean set(java.lang.String key, java.lang.Object value, java.lang.Integer hashCode)
          Stores data on the server; only the key and the value are specified.
private  boolean set(java.lang.String cmdname, java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode, boolean asString)
          Stores data to cache.
 void setCompressEnable(boolean compressEnable)
          Enable storing compressed data, provided it meets the threshold requirements.
 void setCompressThreshold(long compressThreshold)
          Sets the required length for data to be considered for compression.
 void setDefaultEncoding(java.lang.String defaultEncoding)
          Sets default String encoding when storing primitives as Strings.
 void setPoolName(java.lang.String poolName)
          Sets the pool that this instance of the client will use.
 void setPrimitiveAsString(boolean primitiveAsString)
          Enables storing primitive types as their String values.
 java.util.Map stats()
          Retrieves stats for all servers.
 java.util.Map stats(java.lang.String[] servers)
          Retrieves stats for passed in servers (or all servers).
private  java.util.Map stats(java.lang.String[] servers, java.lang.String command, java.lang.String lineStart)
           
 java.util.Map statsCacheDump(int slabNumber)
          Retrieves items cachedump for all servers.
 java.util.Map statsCacheDump(java.lang.String[] servers, int slabNumber)
          Retrieves stats for passed in servers (or all servers).
 java.util.Map statsItems()
          Retrieves stats items for all servers.
 java.util.Map statsItems(java.lang.String[] servers)
          Retrieves stats for passed in servers (or all servers).
 java.util.Map statsSlabs()
          Retrieves stats items for all servers.
 java.util.Map statsSlabs(java.lang.String[] servers)
          Retrieves stats for passed in servers (or all servers).
 boolean storeCounter(java.lang.String key, long counter)
          Store a counter to memcached given a key
 boolean storeCounter(java.lang.String key, java.lang.Long counter)
          Store a counter to memcached given a key
 boolean storeCounter(java.lang.String key, java.lang.Long counter, java.lang.Integer hashCode)
          Store a counter to memcached given a key

 

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

 

Field Detail

log

private static Logger log

VALUE

private static final java.lang.String VALUE
See Also:
Constant Field Values

STATS

private static final java.lang.String STATS
See Also:
Constant Field Values

ITEM

private static final java.lang.String ITEM
See Also:
Constant Field Values

DELETED

private static final java.lang.String DELETED
See Also:
Constant Field Values

NOTFOUND

private static final java.lang.String NOTFOUND
See Also:
Constant Field Values

STORED

private static final java.lang.String STORED
See Also:
Constant Field Values

NOTSTORED

private static final java.lang.String NOTSTORED
See Also:
Constant Field Values

OK

private static final java.lang.String OK
See Also:
Constant Field Values

END

private static final java.lang.String END
See Also:
Constant Field Values

ERROR

private static final java.lang.String ERROR
See Also:
Constant Field Values

CLIENT_ERROR

private static final java.lang.String CLIENT_ERROR
See Also:
Constant Field Values

SERVER_ERROR

private static final java.lang.String SERVER_ERROR
See Also:
Constant Field Values

COMPRESS_THRESH

private static final int COMPRESS_THRESH
See Also:
Constant Field Values

F_COMPRESSED

private static final int F_COMPRESSED
See Also:
Constant Field Values

F_SERIALIZED

private static final int F_SERIALIZED
See Also:
Constant Field Values

primitiveAsString

private boolean primitiveAsString

compressEnable

private boolean compressEnable

compressThreshold

private long compressThreshold

defaultEncoding

private java.lang.String defaultEncoding

poolName

private java.lang.String poolName

classLoader

private java.lang.ClassLoader classLoader

Constructor Detail

MemCachedClient

public MemCachedClient()
Creates a new instance of MemCachedClient.


MemCachedClient

public MemCachedClient(java.lang.ClassLoader classLoader)
Creates a new instance of MemCacheClient but acceptes a passed in ClassLoader.

Parameters:
classLoader – ClassLoader object.

Method Detail

init

private void init()
Initializes client object to defaults. This enables compression and sets compression threshhold to 15 KB.


setPoolName

public void setPoolName(java.lang.String poolName)
Sets the pool that this instance of the client will use. The pool must already be initialized or none of this will work.

Parameters:
poolName – name of the pool to use

setPrimitiveAsString

public void setPrimitiveAsString(boolean primitiveAsString)
Enables storing primitive types as their String values.

Parameters:
primitiveAsString – if true, then store all primitives as their string value.

setDefaultEncoding

public void setDefaultEncoding(java.lang.String defaultEncoding)
Sets default String encoding when storing primitives as Strings. Default is UTF-8.

Parameters:
defaultEncoding

setCompressEnable

public void setCompressEnable(boolean compressEnable)
Enable storing compressed data, provided it meets the threshold requirements. If enabled, data will be stored in compressed form if it is
longer than the threshold length set with setCompressThreshold(int)

The default is that compression is enabled.

Even if compression is disabled, compressed data will be automatically
decompressed.

Parameters:
compressEnabletrue to enable compression, false to disable compression

setCompressThreshold

public void setCompressThreshold(long compressThreshold)
Sets the required length for data to be considered for compression. If the length of the data to be stored is not equal or larger than this value, it will not be compressed. This defaults to 15 KB.

Parameters:
compressThreshold – required length of data to consider compression

keyExists

public boolean keyExists(java.lang.String key)
Checks to see if key exists in cache.

Parameters:
key – the key to look for
Returns:
true if key found in cache, false if not (or if cache is down)

delete

public boolean delete(java.lang.String key)
Deletes an object from cache given cache key.

Parameters:
key – the key to be removed
Returns:
true, if the data was deleted successfully

delete

public boolean delete(java.lang.String key,                      java.util.Date expiry)
Deletes an object from cache given cache key and expiration date.

Parameters:
key – the key to be removed
expiry – when to expire the record.
Returns:
true, if the data was deleted successfully

delete

public boolean delete(java.lang.String key,                      java.lang.Integer hashCode,                      java.util.Date expiry)
Deletes an object from cache given cache key, a delete time, and an optional hashcode. The item is immediately made non retrievable.
Keep in mind add and replace
will fail when used with the same key will fail, until the server reaches the
specified time. However, set will succeed,
and the new value will not be deleted.

Parameters:
key – the key to be removed
hashCode – if not null, then the int hashcode to use
expiry – when to expire the record.
Returns:
true, if the data was deleted successfully

set

public boolean set(java.lang.String key,                   java.lang.Object value)
Stores data on the server; only the key and the value are specified.

Parameters:
key – key to store data under
value – value to store
Returns:
true, if the data was successfully stored

set

public boolean set(java.lang.String key,                   java.lang.Object value,                   java.lang.Integer hashCode)
Stores data on the server; only the key and the value are specified.

Parameters:
key – key to store data under
value – value to store
hashCode – if not null, then the int hashcode to use
Returns:
true, if the data was successfully stored

set

public boolean set(java.lang.String key,                   java.lang.Object value,                   java.util.Date expiry)
Stores data on the server; the key, value, and an expiration time are specified.

Parameters:
key – key to store data under
value – value to store
expiry – when to expire the record
Returns:
true, if the data was successfully stored

set

public boolean set(java.lang.String key,                   java.lang.Object value,                   java.util.Date expiry,                   java.lang.Integer hashCode)
Stores data on the server; the key, value, and an expiration time are specified.

Parameters:
key – key to store data under
value – value to store
expiry – when to expire the record
hashCode – if not null, then the int hashcode to use
Returns:
true, if the data was successfully stored

add

public boolean add(java.lang.String key,                   java.lang.Object value)
Adds data to the server; only the key and the value are specified.

Parameters:
key – key to store data under
value – value to store
Returns:
true, if the data was successfully stored

add

public boolean add(java.lang.String key,                   java.lang.Object value,                   java.lang.Integer hashCode)
Adds data to the server; the key, value, and an optional hashcode are passed in.

Parameters:
key – key to store data under
value – value to store
hashCode – if not null, then the int hashcode to use
Returns:
true, if the data was successfully stored

add

public boolean add(java.lang.String key,                   java.lang.Object value,                   java.util.Date expiry)
Adds data to the server; the key, value, and an expiration time are specified.

Parameters:
key – key to store data under
value – value to store
expiry – when to expire the record
Returns:
true, if the data was successfully stored

add

public boolean add(java.lang.String key,                   java.lang.Object value,                   java.util.Date expiry,                   java.lang.Integer hashCode)
Adds data to the server; the key, value, and an expiration time are specified.

Parameters:
key – key to store data under
value – value to store
expiry – when to expire the record
hashCode – if not null, then the int hashcode to use
Returns:
true, if the data was successfully stored

replace

public boolean replace(java.lang.String key,                       java.lang.Object value)
Updates data on the server; only the key and the value are specified.

Parameters:
key – key to store data under
value – value to store
Returns:
true, if the data was successfully stored

replace

public boolean replace(java.lang.String key,                       java.lang.Object value,                       java.lang.Integer hashCode)
Updates data on the server; only the key and the value and an optional hash are specified.

Parameters:
key – key to store data under
value – value to store
hashCode – if not null, then the int hashcode to use
Returns:
true, if the data was successfully stored

replace

public boolean replace(java.lang.String key,                       java.lang.Object value,                       java.util.Date expiry)
Updates data on the server; the key, value, and an expiration time are specified.

Parameters:
key – key to store data under
value – value to store
expiry – when to expire the record
Returns:
true, if the data was successfully stored

replace

public boolean replace(java.lang.String key,                       java.lang.Object value,                       java.util.Date expiry,                       java.lang.Integer hashCode)
Updates data on the server; the key, value, and an expiration time are specified.

Parameters:
key – key to store data under
value – value to store
expiry – when to expire the record
hashCode – if not null, then the int hashcode to use
Returns:
true, if the data was successfully stored

set

private boolean set(java.lang.String cmdname,                    java.lang.String key,                    java.lang.Object value,                    java.util.Date expiry,                    java.lang.Integer hashCode,                    boolean asString)
Stores data to cache. If data does not already exist for this key on the server, or if the key is being
deleted, the specified value will not be stored.
The server will automatically delete the value when the expiration time has been reached.

If compression is enabled, and the data is longer than the compression threshold
the data will be stored in compressed form.

As of the current release, all objects stored will use java serialization.

Parameters:
cmdname – action to take (set, add, replace)
key – key to store cache under
value – object to cache
expiry – expiration
hashCode – if not null, then the int hashcode to use
asString – store this object as a string?
Returns:
true/false indicating success

storeCounter

public boolean storeCounter(java.lang.String key,                            long counter)
Store a counter to memcached given a key

Parameters:
key – cache key
counter – number to store
Returns:
true/false indicating success

storeCounter

public boolean storeCounter(java.lang.String key,                            java.lang.Long counter)
Store a counter to memcached given a key

Parameters:
key – cache key
counter – number to store
Returns:
true/false indicating success

storeCounter

public boolean storeCounter(java.lang.String key,                            java.lang.Long counter,                            java.lang.Integer hashCode)
Store a counter to memcached given a key

Parameters:
key – cache key
counter – number to store
hashCode – if not null, then the int hashcode to use
Returns:
true/false indicating success

getCounter

public long getCounter(java.lang.String key)
Returns value in counter at given key as long.

Parameters:
key – cache ket
Returns:
counter value or -1 if not found

getCounter

public long getCounter(java.lang.String key,                       java.lang.Integer hashCode)
Returns value in counter at given key as long.

Parameters:
key – cache ket
hashCode – if not null, then the int hashcode to use
Returns:
counter value or -1 if not found

incr

public long incr(java.lang.String key)
Increment the value at the specified key by 1, and then return it.

Parameters:
key – key where the data is stored
Returns:
-1, if the key is not found, the value after incrementing otherwise

incr

public long incr(java.lang.String key,                 long inc)
Increment the value at the specified key by passed in val.

Parameters:
key – key where the data is stored
inc – how much to increment by
Returns:
-1, if the key is not found, the value after incrementing otherwise

incr

public long incr(java.lang.String key,                 long inc,                 java.lang.Integer hashCode)
Increment the value at the specified key by the specified increment, and then return it.

Parameters:
key – key where the data is stored
inc – how much to increment by
hashCode – if not null, then the int hashcode to use
Returns:
-1, if the key is not found, the value after incrementing otherwise

decr

public long decr(java.lang.String key)
Decrement the value at the specified key by 1, and then return it.

Parameters:
key – key where the data is stored
Returns:
-1, if the key is not found, the value after incrementing otherwise

decr

public long decr(java.lang.String key,                 long inc)
Decrement the value at the specified key by passed in value, and then return it.

Parameters:
key – key where the data is stored
inc – how much to increment by
Returns:
-1, if the key is not found, the value after incrementing otherwise

decr

public long decr(java.lang.String key,                 long inc,                 java.lang.Integer hashCode)
Decrement the value at the specified key by the specified increment, and then return it.

Parameters:
key – key where the data is stored
inc – how much to increment by
hashCode – if not null, then the int hashcode to use
Returns:
-1, if the key is not found, the value after incrementing otherwise

incrdecr

private long incrdecr(java.lang.String cmdname,                      java.lang.String key,                      long inc,                      java.lang.Integer hashCode)
Increments/decrements the value at the specified key by inc. Note that the server uses a 32-bit unsigned integer, and checks for
underflow. In the event of underflow, the result will be zero. Because
Java lacks unsigned types, the value is returned as a 64-bit integer.
The server will only decrement a value if it already exists;
if a value is not found, -1 will be returned.

Parameters:
cmdname – increment/decrement
key – cache key
inc – amount to incr or decr
hashCode – if not null, then the int hashcode to use
Returns:
new value or -1 if not exist

get

public java.lang.Object get(java.lang.String key)
Retrieve a key from the server, using a specific hash. If the data was compressed or serialized when compressed, it will automatically
be decompressed or serialized, as appropriate. (Inclusive or)

Non-serialized data will be returned as a string, so explicit conversion to
numeric types will be necessary, if desired

Parameters:
key – key where data is stored
Returns:
the object that was previously stored, or null if it was not previously stored

get

public java.lang.Object get(java.lang.String key,                            java.lang.Integer hashCode)
Retrieve a key from the server, using a specific hash. If the data was compressed or serialized when compressed, it will automatically
be decompressed or serialized, as appropriate. (Inclusive or)

Non-serialized data will be returned as a string, so explicit conversion to
numeric types will be necessary, if desired

Parameters:
key – key where data is stored
hashCode – if not null, then the int hashcode to use
Returns:
the object that was previously stored, or null if it was not previously stored

get

public java.lang.Object get(java.lang.String key,                            java.lang.Integer hashCode,                            boolean asString)
Retrieve a key from the server, using a specific hash. If the data was compressed or serialized when compressed, it will automatically
be decompressed or serialized, as appropriate. (Inclusive or)

Non-serialized data will be returned as a string, so explicit conversion to
numeric types will be necessary, if desired

Parameters:
key – key where data is stored
hashCode – if not null, then the int hashcode to use
asString – if true, then return string val
Returns:
the object that was previously stored, or null if it was not previously stored

getMultiArray

public java.lang.Object[] getMultiArray(java.lang.String[] keys)
Retrieve multiple objects from the memcache. This is recommended over repeated calls to get(), since it
is more efficient.

Parameters:
keys – String array of keys to retrieve
Returns:
Object array ordered in same order as key array containing results

getMultiArray

public java.lang.Object[] getMultiArray(java.lang.String[] keys,                                        java.lang.Integer[] hashCodes)
Retrieve multiple objects from the memcache. This is recommended over repeated calls to get(), since it
is more efficient.

Parameters:
keys – String array of keys to retrieve
hashCodes – if not null, then the Integer array of hashCodes
Returns:
Object array ordered in same order as key array containing results

getMultiArray

public java.lang.Object[] getMultiArray(java.lang.String[] keys,                                        java.lang.Integer[] hashCodes,                                        boolean asString)
Retrieve multiple objects from the memcache. This is recommended over repeated calls to get(), since it
is more efficient.

Parameters:
keys – String array of keys to retrieve
hashCodes – if not null, then the Integer array of hashCodes
asString – if true, retrieve string vals
Returns:
Object array ordered in same order as key array containing results

getMulti

public java.util.Map getMulti(java.lang.String[] keys)
Retrieve multiple objects from the memcache. This is recommended over repeated calls to get(), since it
is more efficient.

Parameters:
keys – String array of keys to retrieve
Returns:
a hashmap with entries for each key is found by the server, keys that are not found are not entered into the hashmap, but attempting to retrieve them from the hashmap gives you null.

getMulti

public java.util.Map getMulti(java.lang.String[] keys,                              java.lang.Integer[] hashCodes)
Retrieve multiple keys from the memcache. This is recommended over repeated calls to get(), since it
is more efficient.

Parameters:
keys – keys to retrieve
hashCodes – if not null, then the Integer array of hashCodes
Returns:
a hashmap with entries for each key is found by the server, keys that are not found are not entered into the hashmap, but attempting to retrieve them from the hashmap gives you null.

getMulti

public java.util.Map getMulti(java.lang.String[] keys,                              java.lang.Integer[] hashCodes,                              boolean asString)
Retrieve multiple keys from the memcache. This is recommended over repeated calls to get(), since it
is more efficient.

Parameters:
keys – keys to retrieve
hashCodes – if not null, then the Integer array of hashCodes
asString – if true then retrieve using String val
Returns:
a hashmap with entries for each key is found by the server, keys that are not found are not entered into the hashmap, but attempting to retrieve them from the hashmap gives you null.

loadItems

private void loadItems(SockIOPool.SockIO sock,                       java.util.Map hm,                       boolean asString)                throws java.io.IOException
This method loads the data from cache into a Map. Pass a SockIO object which is ready to receive data and a HashMap
to store the results.

Parameters:
sock – socket waiting to pass back data
hm – hashmap to store data into
asString – if true, and if we are using NativehHandler, return string val
Throws:
java.io.IOException – if io exception happens while reading from socket

flushAll

public boolean flushAll()
Invalidates the entire cache. Will return true only if succeeds in clearing all servers.

Returns:
success true/false

flushAll

public boolean flushAll(java.lang.String[] servers)
Invalidates the entire cache. Will return true only if succeeds in clearing all servers. If pass in null, then will try to flush all servers.

Parameters:
servers – optional array of host(s) to flush (host:port)
Returns:
success true/false

stats

public java.util.Map stats()
Retrieves stats for all servers. Returns a map keyed on the servername. The value is another map which contains stats with stat name as key and value as value.

Returns:
Stats map

stats

public java.util.Map stats(java.lang.String[] servers)
Retrieves stats for passed in servers (or all servers). Returns a map keyed on the servername. The value is another map which contains stats with stat name as key and value as value.

Parameters:
servers – string array of servers to retrieve stats from, or all if this is null
Returns:
Stats map

statsItems

public java.util.Map statsItems()
Retrieves stats items for all servers. Returns a map keyed on the servername. The value is another map which contains item stats with itemname:number:field as key and value as value.

Returns:
Stats map

statsItems

public java.util.Map statsItems(java.lang.String[] servers)
Retrieves stats for passed in servers (or all servers). Returns a map keyed on the servername. The value is another map which contains item stats with itemname:number:field as key and value as value.

Parameters:
servers – string array of servers to retrieve stats from, or all if this is null
Returns:
Stats map

statsSlabs

public java.util.Map statsSlabs()
Retrieves stats items for all servers. Returns a map keyed on the servername. The value is another map which contains slabs stats with slabnumber:field as key and value as value.

Returns:
Stats map

statsSlabs

public java.util.Map statsSlabs(java.lang.String[] servers)
Retrieves stats for passed in servers (or all servers). Returns a map keyed on the servername. The value is another map which contains slabs stats with slabnumber:field as key and value as value.

Parameters:
servers – string array of servers to retrieve stats from, or all if this is null
Returns:
Stats map

statsCacheDump

public java.util.Map statsCacheDump(int slabNumber)
Retrieves items cachedump for all servers. Returns a map keyed on the servername. The value is another map which contains cachedump stats with the cachekey as key and byte size and unix timestamp as value.

Parameters:
slabNumber – the item number of the cache dump
Returns:
Stats map

statsCacheDump

public java.util.Map statsCacheDump(java.lang.String[] servers,                                    int slabNumber)
Retrieves stats for passed in servers (or all servers). Returns a map keyed on the servername. The value is another map which contains cachedump stats with the cachekey as key and byte size and unix timestamp as value.

Parameters:
servers – string array of servers to retrieve stats from, or all if this is null
slabNumber – the item number of the cache dump
Returns:
Stats map

stats

private java.util.Map stats(java.lang.String[] servers,                            java.lang.String command,                            java.lang.String lineStart)
2006年08月07日

SQUID防图片,MP3盗链

Squid 提供强大的acl机制,能够方便地做到图片,MP3防盗链。下面是相关配置(以mop.com为例):

acl legalreferer referer_regex ^http://2006.mop.com ^http://[a-z].*\.mop\.comacl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$ \.rar$ \.wmv$ \.mp3$ \.rm$ \.avi$ \.asf$ \.mpg$ \.mpeg$ \.rmvb$ \.wma$http_access deny !legalreferer picurl

如还想允许对视频、音频文件在线播放,就用下面的配置:

acl legalreferer referer_regex ^http://2006.mop.com/ ^http://[a-z].*\.mop\.comacl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$ \.rar$http_access deny !legalreferer picurlacl legalplayer browser -i ^nsplayer ^contype$ ^rma ^windows-media-player ^foobar2000acl wmvurl url_regex -i \.wmv$ \.mp3$ \.rm$ \.avi$ \.asf$ \.mpg$ \.mpeg$ \.rmvb$ \.wma$http_access allow legalplayer wmvurlhttp_access deny !legalreferer wmvurl

SQUID refresh_pattern详解

refresh_pattern 大概是 squid 最有意思但最不好懂的配置参数了,经过看书,大概明白如何使用,写出来贡献。

记住refresh_pattern 只对后端没设置Expires过期时间的页面起作用,比如论坛页面;而对类似apache mod_expires 设置过的页面不起作用。

说明之前,先将个概念LM,LM就是页面Header里时间(Date)和Last-Modified时间的差。Date一般是Squid从后面取页面的时间,Last-Modified 一般是页面生成时间。

refresh_pattern 的语法是

refresh_pattern [-i] regexp min percent max [options]

regexp 就不讲了,大家都明白的;)

min, max的单位是分钟,percent就是百分比。

refresh_pattern 的算法如下:(当前时间定义为CURRENT_DATE)
1) If ((CURRENT_DATE-DATE(就是LM里定义的时间)) < min),cache是新鲜的
2) else if ((CURRENT_DATE-DATE) < (min + (max-min)*percent),cache是新鲜的
3) else cache是过期的
cache过期就需要从后面server取新鲜内容。
<img src="[External Link]http://wk.mop.com/UploadFile/files.xml?action=download&file=refresh_pattern.png">
如果希望页面一进入cache就不删除,直到被主动purge掉为止,可以加上ignore-reload选项
一般情况可以使用 reload-into-ims。
举例:

refresh_pattern -i \.gif$   1440    50%     2880      ignore-reloadrefresh_pattern -i \.jpg$   1440    50%     2880      ignore-reloadrefresh_pattern -i \.png$   1440    50%     2880      ignore-reload

refresh_pattern -i \.mp3$   1440    50%     2880      ignore-reloadrefresh_pattern -i \.wmv$   1440    50%     2880      ignore-reloadrefresh_pattern -i \.rm$   1440    50%     2880      ignore-reloadrefresh_pattern -i \.swf$   1440    50%     2880      ignore-reloadrefresh_pattern -i \.mpeg$   1440    50%     2880      ignore-reloadrefresh_pattern -i \.wma$   1440    50%     2880      ignore-reload

refresh_pattern -i \.css$ 10 50% 60 reload-into-imsrefresh_pattern -i \.js$ 10 50% 60 reload-into-imsrefresh_pattern -i \.xml$ 10 50% 30 reload-into-ims
Squid优化(一)

Hot!几个SQUID重要参数:

maximum_object_size 是 能cache最大的文件大小。对应wmv,rm文件,建议设置为32768 kB
maximum_object_size_in_memory 是在内存中cache的最大文件大小。
cache_mem 是SQUID可用到的最大内存。经实践,4G内存的服务器用2G;超过2G导致SQUID运行不稳

首先要分析SQUID所cache内容:

运行

squidclient -p 80 cache_object://localhost/info

能看到如下内容:

Storage Swap size: 7549104 KB
Storage Mem size: 418804 KB
Mean Object Size: 160.46 KB

Mean Object Size是平均内容大小,一般要把maximum_object_size_in_memory设置成离它最近的128的倍数。在这个例子中maximum_object_size_in_memory 的值应该是256kB。

cache_mem 一般设置成服务器内存的一半或更多,只要运行过程中LINUX没有使用SWAP就可以。

再就是按业务分SQUID。
比如某个论坛,用户能上载图片和视频;当然我们要把上载的图片、视频放在单独的域名上,比如img.example.com, video.example.com;这两个域名只提供静态文件服务。

根据统计,图片的平均大小在100KB,视频的平均大小在4M,差别是很大,应该建两个squid分别作图片和视频的CACHE。图片SQUID的 maximum_object_size_in_memory 设置为256KB,视频的SQUID的maximum_object_size_in_memory设置为8196KB。

Squid优化(2)

Hot!探讨动态内容的CACHE。

BBS,论坛是典型动态内容,要保证内容更新及时的同时,提高访问速度,降低数据库负担不是个简单任务。经实践发现如下办法取得很好效果:

1) 配置SQUID,对动态内容强制CACHE,用到的配置参数是refresh_pattern

refresh_pattern ^/forum/viewthread.php 1440 1000% 1440 ignore-reload

/forum/viewthread.php的内容将强制保持1天

2) 修改论坛程序在用户回复帖子后,向SQUID发送PURGE命令清除相应帖子的页面CACHE,保证失效性
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~实现过这一功能,但是有时候生效,有时候无效,还未进一步查明原因.(Edit by Sean)  

3) 有些频繁更新的页面可以不CACHE,用no_cache参数

acl no_forum_cache urlpath_regex ^/forum/forumdisplay.php&lt;br /&gt;no_cache DENY no_forum_cache

差不多学习了2周的squid,向高手请教,网上查找资料,学习例子,现在做个小小的总结。

squid这东西有2个用处:1、代理服务器;2、web服务器缓存。我学习的是后者。

什么安装,目录结构就不多说了

说一说配置的注意事项:
————————————————-
http_port 80
————————————————-
squid监听的端口,不能和服务器上目前的服务端口有冲突。需要特别注意,当时吃了不少亏。

————————————————-
cache_mem 32 MB
maximum_object_size 32768 KB
maximum_object_size_in_memory 16 KB
cache_dir ufs /data/squid/var/cache 500 16 256
————————————————–
存储策略,目前还没有深入涉及到,以后再看。

—————————————–
refresh_pattern
—————————————–
关于refresh_pattern,可以参见阙老大的文章,受益匪浅,过一会儿转过来,哈哈。

—————————————–
acl
http_access
—————————————–
第一个制定位置/端口/域名,第二个制定是否allow还是deny!!
在这里吃过亏,记牢点!!恩恩。

—————————————–
httpd_accel_port 8080
httpd_accel_single_host off
httpd_accel_with_proxy off
httpd_accel_uses_host_header on
——————————————
以上为真正的web服务器的运行端口。

—————————————–
acl PURGE method PURGE
acl llhost src 127.0.0.1/255.255.255.255
http_access allow PURGE llhost
—————————————–
PURGE!!掉一个页面咯!

启动:./squid -Cd1
修改:./squid -k reconfigre
暂停:./squid -k kill

如何去purge一个页面呢?
1、使用squidclient
./squidclient -p 80 mgr:objects   
查看缓存了哪些页面
./squidclient -p 80 -m purge http://*.*.*.*:8080/XXX.jsp
purge某页
2、发送http请求
——————————
PURGE /XXX.jsp HTTP/1.1
Host: *.*.*.*
Connection: close
——————————
这个为http头请求;编写代码
Socket server=new Socket(ipaddr,iport);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
write.write(purgestr);
write.flush();
write.close();
server.close();

就可purge掉页面!!恩恩。