SqlLiteCache.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.yc.videosqllite.disk;
  2. import com.yc.videosqllite.manager.CacheConfig;
  3. import com.yc.videosqllite.model.VideoLocation;
  4. import com.yc.videosqllite.utils.CacheLogUtils;
  5. import com.yc.videosqllite.utils.VideoMd5Utils;
  6. import java.io.File;
  7. import java.io.IOException;
  8. /**
  9. * <pre>
  10. * @author yangchong
  11. * email : yangchong211@163.com
  12. * time : 2020/8/6
  13. * desc : 磁盘缓存工具
  14. * revise:
  15. * </pre>
  16. */
  17. public class SqlLiteCache {
  18. private CacheConfig cacheConfig;
  19. private DiskLruCache diskLruCache;
  20. public SqlLiteCache(CacheConfig cacheConfig) {
  21. this.cacheConfig = cacheConfig;
  22. File path = DiskFileUtils.getFilePath(cacheConfig.getContext());
  23. String pathString = path.getPath();
  24. CacheLogUtils.d("SqlLiteCache-----pathString-"+pathString);
  25. try {
  26. diskLruCache = DiskLruCache.open(path,1,1,cacheConfig.getCacheMax());
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. /**
  32. * 存数据
  33. * @param url 链接
  34. * @param location 视频数据
  35. */
  36. public synchronized void put(String url , VideoLocation location){
  37. if (url==null || url.length()==0){
  38. return;
  39. }
  40. if (location==null){
  41. return;
  42. }
  43. if (diskLruCache!=null){
  44. try {
  45. String key = VideoMd5Utils.encryptMD5ToString(url, cacheConfig.getSalt());
  46. CacheLogUtils.d("SqlLiteCache-----put--key--"+key);
  47. DiskLruCache.Editor editor = diskLruCache.edit(key);
  48. if (editor!=null){
  49. String json = location.toJson();
  50. editor.set(0,json);
  51. CacheLogUtils.d("SqlLiteCache-----put--json--"+json);
  52. editor.commit();
  53. }
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. } finally {
  57. try {
  58. diskLruCache.flush();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * 取数据
  67. * @param url 链接
  68. * @return
  69. */
  70. public synchronized long get(String url){
  71. if (url==null || url.length()==0){
  72. return -1;
  73. }
  74. if (diskLruCache!=null){
  75. String key = VideoMd5Utils.encryptMD5ToString(url, cacheConfig.getSalt());
  76. CacheLogUtils.d("SqlLiteCache-----get--key-"+key);
  77. try {
  78. DiskLruCache.Value value = diskLruCache.get(key);
  79. if (value != null) {
  80. File file = value.getFile(0);
  81. long length = value.getLength(0);
  82. String string = value.getString(0);
  83. VideoLocation location = VideoLocation.toObject(string);
  84. long position = location.getPosition();
  85. CacheLogUtils.d("SqlLiteCache-----get---"+file+"-------"+length+"-------"+string+"-----"+position);
  86. return position;
  87. }
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. } finally {
  91. try {
  92. diskLruCache.flush();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. return -1;
  99. }
  100. /**
  101. * 移除数据
  102. * @param url 链接
  103. * @return
  104. */
  105. public synchronized boolean remove(String url){
  106. if (diskLruCache!=null){
  107. String key = VideoMd5Utils.encryptMD5ToString(url, cacheConfig.getSalt());
  108. try {
  109. boolean remove = diskLruCache.remove(key);
  110. return remove;
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. } finally {
  114. try {
  115. diskLruCache.flush();
  116. } catch (IOException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * 是否包含
  125. * @param url 链接
  126. * @return
  127. */
  128. public synchronized boolean containsKey(String url){
  129. if (diskLruCache!=null){
  130. String key = VideoMd5Utils.encryptMD5ToString(url, cacheConfig.getSalt());
  131. try {
  132. DiskLruCache.Value value = diskLruCache.get(key);
  133. return value != null;
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. } finally {
  137. try {
  138. diskLruCache.flush();
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. }
  144. return false;
  145. }
  146. /**
  147. * 清楚所有数据
  148. * @return 是否清楚完毕
  149. */
  150. public synchronized boolean clearAll(){
  151. if (diskLruCache!=null){
  152. try {
  153. diskLruCache.delete();
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. } finally {
  157. try {
  158. diskLruCache.flush();
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. }