11月 09

用nginx的cache 对本地静态文件做缓存

需要对静态文件做缓存,但是静态文件也是在本地,由同一个nginx来解析。
使用if不能嵌套,所以只能使用本地的IP跳转一下,我的cache使用的nginx命名cache_one配置如下。如果本地跳转有问题可以再hosts表中修改一下指向。

   #######################pic
   upstream  local_img {
            server localhost:81;
       }
    server{
        listen       81;
        server_name 127.0.0.1;
        location / {
                root /image/;
                client_max_body_size   10m;
                access_log off;
                autoindex off;
                }
    }
   server {
        listen       80;
        server_name img.test.com;
        proxy_cache cache_one;
        location / {
                proxy_redirect off;
                proxy_cache_valid 200 304 12h;
                proxy_cache_valid 301 302 1m;
                proxy_cache_key $host$uri$is_args$args;
                add_header X-Cache $upstream_cache_status;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://local_img;
                access_log off;
                }
    }
5月 27

python连接oracle和mysql备忘

import cx_Oracle
    
def sql_comm(sql_run):
    db = cx_Oracle.connect(‘user’, ‘passwd’, ‘IP:port/sid’) 
    try:
        cursor=db.cursor()
    except cx_Oracle.ERROR,e:
        print “Error %d:%s”%(e.args[0],e.args[1])
    try:
           cursor.execute(sql_run)
    result_set=cursor.fetchall()
           cursor.close()
           db.commit()
           db.close()
    return result_set
    except e:
        print “Error %s”%(e.args[0])
        cursor.close()
        db.close()

python连接mysql

import MySQLdb

def sql_comm(sql_run):
        try:
                conn=MySQLdb.connect(host=host,user=username,passwd=pwd,db=database)
                cursor = conn.cursor()
        except MySQLdb.Error,e:
                print “Error %d:%s”%(e.args[0],e.args[1])
        try:
                cursor.execute(sql_run)
                result_set=cursor.fetchall()
                cursor.close()
                db.close()
                return result_set
        except MySQLdb.Error,e:
                print “Error %d:%s”%(e.args[0],e.args[1])
                cursor.close()
                db.close()

注意部分,在mysql修改数据时最好使用commit,如果只是select就不用了,不然得不到数据。
在oracle和mysql插入大量数据时,可以使用executemany,需要注意的是,oracle插入大量
数据时要将数据类型为元组,然后放到列表中。建议一次插入10000行。