android:缓存服务器数据为本地存储

文章地址:

两个开源代码

也足够用了,没必要自己去写,文件很小 – reservoir 缓存对象为字符串; – disklrucache 存取sd卡工具;

实现想法

也就是将接口的字符串缓存到本地而已;不一定是网络缓存,可以指定任何想要的字符串保存,如果愿意可以用数据库等等,看需要咯,减轻服务器加载压力

由于很简单这个可用花点时间做的更好点,比如不同接口不同时间,完全使用缓存还是缓存过期后仍使用缓存,等待网络加载完成后重新写入等等看需要;

不同项目不同网络层,网络加载的使用之前写过一个,在这里:

简单实现的一些简化做法

对于缓存的额外信息需要的不是很大,所以判断换存时间的方式不是Map

initialCapacity = 1*1024*1024;String key_time_mills = “timeMills”;private static LinkedHashMap<String, Long> cacheTimeMills;String key_time_valid = “timeValid”;private static LinkedHashMap<String, Long> cacheTimeValid;

改写接口代码:

(final Context context, final boolean isShow, final String content,PostUrlInfo postUrlInfo,final RequestParams params, final IParser iParse, final INetCallBack callBack){;//尝试获取缓存数据,如果缓存未过期,重点在get方法里;String response = Reservoir.get(postUrlInfo.reqIndentKey, String.class);if(TextUtils.isEmpty(response)) return false;//直接使用网络层正常解析方法,但标记为缓存加载//不在放入缓存,做一些处理;DialogFragment dialogFragment = new ProgressDialogFragment(content);ParseResult parseResult = new ParseResult();parseResult.isNetParse = false;parseTask task = new parseTask(dialogFragment, iParse, callBack, 0,getDefaultHeaders(params), response,postUrlInfo,parseResult);task.execute();;}(final Context context, final boolean isShow, final String content,String relativePath,final RequestParams params, final IParser iParse, final INetCallBack callBack) {PostUrlInfo urlInfo = new PostUrlInfo(BaseURL,relativePath);post(context, isShow, content,urlInfo, params, iParse, callBack);}(long cacheTime,final Context context, final boolean isShow, final String content,String relativePath,final RequestParams params, final IParser iParse, final INetCallBack callBack) {//也可以使用host:port:ralaUrl的方式,已封装看情况;PostUrlInfo urlInfo = new PostUrlInfo(BaseURL,relativePath);urlInfo.setDefaultCacheTime(cacheTime);post(context, isShow, content,urlInfo, params, iParse, callBack);}(final Context context, final boolean isShow, final String content,final PostUrlInfo postUrlInfo,final RequestParams params, final IParser iParse, final INetCallBack callBack) {if(params == null) {return;}Logger.i(postUrlInfo.getPostUrl());Logger.i(params.toString());//计算该次接口的key值,检查缓存,getReqIdentifiter可去除要计算//的postKey,比如单次随机数等;postUrlInfo.reqIndentKey =getReqIdentifiter(postUrlInfo,params);if(postCheck(context, isShow, content,postUrlInfo,params, iParse, callBack)){return ;}client.post(context, postUrlInfo.getPostUrl(), (org.apache.http.Header[]) getDefaultHeaders(params), params,null, new AsyncHttpResponseHandler() {};

一个JAVA AES加密的代码文件 EnDecrypt.java

package com.anupcowkur.reservoir;import java.io.UnsupportedEncodingException;import java.math.BigInteger;import java.security.InvalidKeyException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;{public static String encrypts(String sourContent,String password){try{byte[] encryptResult = encrypt(sourContent, password);return parseByte2HexStr(encryptResult);}catch(Exception ex){return sourContent;}}public static String decrypts(String cryContent,String password){try{byte[] decryptFrom = parseHexStr2Byte(cryContent);byte[] decryptResult = decrypt(decryptFrom,password);return new String(decryptResult);}catch(Exception ex){return cryContent;}}[] encrypt(String content, String password) throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {KeyGenerator kgen = KeyGenerator.getInstance(“AES”);kgen.init(128, new SecureRandom(password.getBytes()));SecretKey secretKey = kgen.generateKey();byte[] enCodeFormat = secretKey.getEncoded();SecretKeySpec key = new SecretKeySpec(enCodeFormat, “AES”);Cipher cipher = Cipher.getInstance(“AES”);// 创建密码器 byte[] byteContent = content.getBytes(“utf-8”);cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(byteContent);return result; // 加密 }[] decrypt(byte[] content, String password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {KeyGenerator kgen = KeyGenerator.getInstance(“AES”);kgen.init(128, new SecureRandom(password.getBytes()));SecretKey secretKey = kgen.generateKey();byte[] enCodeFormat = secretKey.getEncoded();SecretKeySpec key = new SecretKeySpec(enCodeFormat, “AES”);Cipher cipher = Cipher.getInstance(“AES”);// 创建密码器 cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(content);return result; // 加密 }private static String parseByte2HexStr(byte buf[]) {StringBuffer sb = new StringBuffer();for (int i = 0; i < buf.length; i++) {String hex = Integer.toHexString(buf[i] & 0xFF);if (hex.length() == 1) {hex = ‘0’ + hex;}sb.append(hex.toUpperCase());}return sb.toString();}[] parseHexStr2Byte(String hexStr) {if (hexStr.length() < 1)return null;byte[] result = new byte[hexStr.length()/2];for (int i = 0;i< hexStr.length()/2; i++) {int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);result[i] = (byte) (high * 16 + low);}return result;}public static String md5(String s) {try {MessageDigest m = MessageDigest.getInstance(“MD5”);m.update(s.getBytes(“UTF-8”));byte[] digest = m.digest();BigInteger bigInt = new BigInteger(1, digest);return bigInt.toString(16);} catch (NoSuchAlgorithmException e) {throw new AssertionError();} catch (UnsupportedEncodingException e) {throw new AssertionError();}}}

,拿望远镜看别人,拿放大镜看自己。

android:缓存服务器数据为本地存储

相关文章:

你感兴趣的文章:

标签云: