Skip to content

Commit

Permalink
add flask cache
Browse files Browse the repository at this point in the history
  • Loading branch information
wxnacy committed Jun 2, 2019
1 parent 6f6eb59 commit c50e2da
Show file tree
Hide file tree
Showing 15 changed files with 378 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ crawler/common

# for jetbrain
*/*.iml
*.iml

# for other
*/winn/*
Expand Down
22 changes: 22 additions & 0 deletions java/leetcode/src/main/java/com/wxnacy/leetcode/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.wxnacy.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Hello world!
*
Expand All @@ -9,5 +14,22 @@ public class App
public static void main( String[] args )
{
System.out.println( "Hello World!" );
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
boolean flag = a.equals(b);
System.out.println(flag);
System.out.println(Arrays.equals(a, b));
a = new int[]{1, 3};

HashMap<Integer, Integer> m = new HashMap<>();
m.put(1, 1);
m.put(2, 1);
Set<Map.Entry<Integer, Integer>> sets = m.entrySet();

for (Map.Entry<Integer, Integer> set : sets) {

System.out.println(set.getKey() + set.getValue());
}
}

}
8 changes: 8 additions & 0 deletions java/leetcode/src/test/java/com/wxnacy/leetcode/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import org.junit.Test;

import java.util.Arrays;

/**
* Unit test for simple App.
*/
Expand All @@ -16,5 +18,11 @@ public class AppTest
public void shouldAnswerWithTrue()
{
assertTrue( true );

int[] a = {1, 2, 3};
Arrays.fill(a, 6);
for(int item: a) {
System.out.println(item);
}
}
}
20 changes: 0 additions & 20 deletions java/leetcode/src/test/java/com/wxnacy/leetcode/Apps.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


/**
* 只出现一次的数字
* 本题分析文章详见 https://wxnacy.com/leetcode/problems/136-single-number/
Expand All @@ -15,8 +21,15 @@ public void shouldAnswerWithTrue()
{
int[] nums = {4, 1, 2, 1, 2};
assertEquals(singleNumber(nums), 4);
int[] nums = {2, 2, 1};
assertEquals(singleNumber(nums), 1);

Map<Integer, int[]> m = new HashMap();
m.put(1, new int[]{2, 2, 1});
m.put(4, new int[]{4, 1, 2, 1, 2});
Set<Map.Entry<Integer, int[]>> sets = m.entrySet();
for (Map.Entry<Integer, int[]> set : sets) {
Integer n = singleNumber(set.getValue());
assertEquals(set.getKey(), n);
}

}

Expand Down
39 changes: 39 additions & 0 deletions java/leetcode/src/test/java/com/wxnacy/leetcode/TwoSumTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.wxnacy.leetcode;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.*;


/**
* 只出现一次的数字
* 本题分析文章详见 https://wxnacy.com/leetcode/problems/136-single-number/
*/
public class TwoSumTest
{
@Test
public void shouldAnswerWithTrue()
{
assertArrayEquals(twoSum(new int[]{2, 7, 9}, 9), new int[]{0, 1});
}

/**
* 一遍哈希表
* 执行用时 : 6 ms, 在Two Sum的Java提交中击败了94.56% 的用户
* 内存消耗 : 37.9 MB, 在Two Sum的Java提交中击败了80.11% 的用户
*/
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (m.containsKey(target - nums[i])) {
return new int[]{m.get(target - nums[i]), i};
}
m.put(nums[i], i);
}
return new int[]{0, 0};
}
}
10 changes: 10 additions & 0 deletions java/simple/ArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class ArrayTest{
public static void main(String args[]){
System.out.println("Hello World");
int[] a = new int[]{1, 2, 3};
// System.out.println(a);
int[] b = {1, 2, 3};
int[] c = new int[4];
c[0] = 3;
}
}
35 changes: 35 additions & 0 deletions python/flask_cache_demo/filesystem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description: 使用文件系统来存储缓存值
# http://www.pythondoc.com/flask-cache/index.html
'''
FileSystemCache – filesystem
使用文件系统来存储缓存值
CACHE_DEFAULT_TIMEOUT
CACHE_DIR
CACHE_THRESHOLD
CACHE_ARGS
CACHE_OPTIONS
'''

from flask import Flask
from flask import jsonify
from flask_caching import Cache
import random

app = Flask(__name__)
cache = Cache(app, config={
"CACHE_TYPE": "filesystem",
"CACHE_DIR": "/Users/wxnacy/Downloads/cache"
})

@app.route('/filesystem')
@cache.cached(timeout=10)
def filesystem():
return jsonify({"num": random.randint(1, 100)})

if __name__ == "__main__":
app.run(debug=True)

73 changes: 73 additions & 0 deletions python/flask_cache_demo/redis_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description: 使用 redis 做缓存
# http://www.pythondoc.com/flask-cache/index.html
'''
RedisCache – redis
相关配置
CACHE_DEFAULT_TIMEOUT
CACHE_KEY_PREFIX
CACHE_REDIS_HOST
CACHE_REDIS_PORT
CACHE_REDIS_PASSWORD
CACHE_REDIS_DB
CACHE_ARGS
CACHE_OPTIONS
CACHE_REDIS_URL
不配置 CACHE_KEY_PREFIX 和 key_prefix 时,默认 key 为 flask_cache_view/{route}
'''

from flask import Flask
from flask import jsonify
from flask import request
from flask_caching import Cache
import random

app = Flask(__name__)
cache = Cache(app, config={
"CACHE_TYPE": "redis",
"CACHE_REDIS_URL": "redis://localhost:6379/0", # redis 地址
"CACHE_DEFAULT_TIMEOUT": 10, # 全局过期时间
# key 前缀,key 为 redis_cache_view/{route}
"CACHE_KEY_PREFIX": "redis_cache_",
})

@app.route('/redis1')
@cache.cached()
def redis1():
'''
key=redis_cache_view//redis1
'''
return jsonify({"num": random.randint(1, 100)})

@app.route('/redis2')
@cache.cached(timeout = 20, key_prefix='customize')
def redis2():
'''
自定义过期时间 20
key=redis_cache_customize
'''
return jsonify({"num": random.randint(1, 100)})

def make_cache_key():
path = request.path
key = str(hash(request.args))
return f'{path}-{key}'


@app.route('/redis3')
@cache.cached(timeout = 20, key_prefix=make_cache_key)
def redis3():
'''
自定义 key
key=redis_cache_/redis3-133156838395276
'''
return jsonify({"num": random.randint(1, 100)})
if __name__ == "__main__":
app.run(debug=True)

33 changes: 33 additions & 0 deletions python/flask_cache_demo/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description: 使用本地Python字典缓存。这不是真正的线程安全。
# http://www.pythondoc.com/flask-cache/index.html
'''
SimpleCache – simple
使用本地Python字典缓存。这不是真正的线程安全。
相关配置
CACHE_DEFAULT_TIMEOUT
CACHE_THRESHOLD
CACHE_ARGS
CACHE_OPTIONS
'''

from flask import Flask
from flask import jsonify
from flask_caching import Cache
import random

app = Flask(__name__)
cache = Cache(app, config={"CACHE_TYPE": "simple"})

@app.route('/simple')
@cache.cached(timeout=10)
def simple():
return jsonify({"num": random.randint(1, 100)})

if __name__ == "__main__":
app.run(debug=True)

42 changes: 28 additions & 14 deletions python/leetcode/15-3sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@
'''

class Solution:
def twoSum(self, nums, target):
m = {}
for i in range(len(nums)):
n = target - nums[i]
if n in m:
return [m[n], i]
m[nums[i]] = i
return [-1, -1]

def threeSum(self, nums: 'List[int]') -> 'List[List[int]]':
res = []
for n in nums:


pass
for i in range(len(nums) - 2):
target = 0 - nums[i]
i1, i2 = self.twoSum(nums[i + 1:], target)
if i1 > -1:
i1 += i + 1
i2 += i + 1
print(i, i1, i2)
res.append([nums[i], nums[i1], nums[i2]])
return res

import unittest

Expand All @@ -43,16 +57,16 @@ def tearDown(self):
def do(self, func):
nums = [-1, 0, 1, 2, -1, -4]
res = [ [-1, 0, 1], [-1, -1, 2] ]
self.assertEqual(nums, res)
nums = [-1, 0, 1]
res = [ [-1, 0, 1]]
self.assertEqual(nums, res)
nums = [-1, 0]
res = [[]]
self.assertEqual(nums, res)
nums = [-1, 0, 1, -1, 0, 1]
res = [ [-1, 0, 1]]
self.assertEqual(nums, res)
self.assertEqual(func(nums), res)
# nums = [-1, 0, 1]
# res = [ [-1, 0, 1]]
# self.assertEqual(func(nums), res)
# nums = [-1, 0]
# res = [[]]
# self.assertEqual(nums, res)
# nums = [-1, 0, 1, -1, 0, 1]
# res = [ [-1, 0, 1]]
# self.assertEqual(func(nums), res)

def test_func(self):
self.do(s.threeSum)
Expand Down
15 changes: 15 additions & 0 deletions python/office_demo/records_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description:

import records

db = records.Database('mysql+pymysql://root:[email protected]:3306/study?charset=utf8mb4')

rows = db.query('select * from user')
print(rows.dataset)
print(rows.first())

# with open('/tmp/report.xls', 'wb') as f:
# f.write(rows.export('xls'))
Loading

0 comments on commit c50e2da

Please sign in to comment.