cache_size

Squid缓存配置详解

CACHE SIZE

Tag Name

cache_mem

Usage

cache_mem bytes

Description
'cache_mem' specifies the ideal amount of memory to be used for :

  • In-Transit objects
  • Hot Objects
  • Negative-Cached objects

Data for these objects are stored in 4 KB blocks. This parameter specifies the ideal upper limit on the total size of 4 KB blocks allocated. In-transit objects have priority over the others. When additional space is needed for incoming data, negative-cached and hot objects will be released. In other words, the negative-cached and hot objects will fill up any unused space not needed for in-transit objects. If circumstances require, this limit will be exceeded. Specifically, if your incoming request rate requires more than 'cache_mem' of memory to hold in-transit objects, Squid will exceed this limit to satisfy the new requests. When the load decreases, blocks will be freed until the high-water mark is reached. Thereafter, blocks will be used to store hot objects.

Default

cache_mem 8 MB

Example
cache_mem 1 GB

Caution
This parameter does not specify the maximum process size. It places a limit on one aspect of squid's memory usage. Squid uses memory for other things as well. Process will probably become twice or three times bigger than the value put here.

Tag Name

cache_swap_low (percent, 0-100)

Usage

cache_swap_low percentage

Description
This tag is used to specify the low-water mark for cache object replacement. Replacement begins when the swap (disk) usage is above the low-water mark and attempts to maintain utilization near the low-water mark. If utilization is close to the low-water mark, less replacement is done each time.

Default

cache_swap_low 90

Example
cache_swap_low 95

Caution
If you have a large cache, 5% could be hundreds of MB. If this is the case, you may wish to set this number closer together.

Tag Name

cache_swap_high (percent, 0-100)

Usage

cache_swap_high percentage

Description
This tag is used to specify the high-water mark for cache object aggressive replacement. As swap utilization gets close to high-water mark, object eviction becomes more aggressive.

Default

cache_swap_high 95

Example
cache_swap_high 98

Caution
If you have a large cache, 5% could be hundreds of MB. If this is the case, you may wish to set this number closer together.

Tag Name

maximum_object_size (bytes)

Usage

maximum_object_size (bytes)

Description
Objects larger than this size will NOT be saved on disk. The value is specified in kilobytes, and the default is 4 MB. If you wish to get a high BYTES hit ratio, you should probably increase this (one 32 MB object hit counts for 3200 10 KB hits). If you wish to increase speed more than your want to save bandwidth, you should leave this low. During the initial downloading of a file, the downloading time seems to be a normal one, but if you download the same file again, the time it takes for download will be very minimum. This indicates that, the file comes from the Cache.

Default

maximum_object_size 4096 KB

Example
maximum_object_size 2000 KB

Caution
If using the LFUDA replacement policy you should increase this value to maximize the byte hit rate improvement of LFUDA. See replacement_policy for a discussion of this policy.

Tag Name

minimum_object_size (bytes)

Usage

minimum_object_size (bytes)

Description
Objects smaller than this size will NOT be saved on disk. The value is specified in kilobytes, and the default is 0 KB, which means there is no minimum. See maximum_object_size for more information.

Default

minimum_object_size 0 KB

Example
minimum_object_size 2000 KB

Note
See maximum_object_size, which is reversibly applicable.

Tag Name

maximum_object_size_in_memory (bytes)

Usage

maximum_object_size_in_memory (bytes)

Description
Objects greater than this size will not be attempted to be kept in the memory cache. This should be set high enough to keep objects accessed frequently in memory to improve performance while, low enough to keep larger objects from hoarding cache_mem.

Default

maximum_object_size_in_memory 8 KB

Example
maximum_object_size_in_memory 20 KB

Tag Name

ipcache_size (number of entries)

Usage

ipcache_size (number of entries)

Description
This tag is used to specify the size of the ipcache.

Default

ipcache_size 1024

Tag Name

ipcache_low (percent)

Usage

ipcache_low percentage

Description
This specifies the low water mark for caching IP addresses.

Default

ipcache_low 90

Tag Name

ipcache_high (percent)

Usage

ipcache_high percentage

Description
This specifies the high water mark for caching IP addresses.

Default

ipcache_high 95

 

Tag Name

fqdncache_size

Usage

fqdncache_size (number of entries)

Description
This specifies the maximum number of FQDN cache entries.

Default

fqdncache_size 1024

 

Tag Name

cache_replacement_policy

Usage

cache_replacement_policy policy

Description
The cache replacement policy parameter decides which objects will remain in cache and which objects are evicted (replaced) to create space for the new objects.

  • LRU : Squid's original list based LRU policy
  • heap GDSF : Greedy-Dual Size Frequency
  • heap LFUDA : Least Frequently Used with Dynamic Aging
  • heap LRU : LRU policy implemented using a heap

This applies to any cache_dir lines listed below this.
The LRU policies keep recently referenced objects. i.e., it replaces the object that has not been accessed for the longest time.

The heap GDSF policy optimizes object-hit rate by keeping smaller popular objects in cache. So it has a better chance of getting a hit. It achieves a lower byte hit rate than LFUDA though, since it evicts larger (possibly popular) objects.

The heap LFUDA ( Least Frequently Used with Dynamic Aging ) policy keeps popular objects in cache regardless of their size and thus optimizes byte hit rate at the expense of hit rate since one large, popular object will prevent many smaller, slightly less popular objects from being cached.

Both policies utilize a dynamic aging mechanism that prevents cache pollution that can otherwise occur with frequency-based replacement policies.

For more information about the GDSF and LFUDA cache replacement policies seehttp://www.hpl.hp.com/techreports/1999/HPL-1999-69.html andhttp://fog.hpl.external.hp.com/techreports/98/HPL-98-173.html .

Default

cache_replacement_policy lru

Example
cache_replacement_policy heap LFUDA
To use this policy, Squid should be built with configure option --enable-removal-policies=heap or simply --enable-removal-policies.

Caution
If using the LFUDA replacement policy, the value of maximum_object_size should be increased above its default of 4096 KB to maximize the potential byte hit rate improvement of LFUDA. If needed to use other policies than default, squid should be built with configure option --enable-removal-policies.

Tag Name

memory_replacement_policy

Usage

memory_replacement_policy policy

Description
The memory replacement policy parameter determines which objects are purged from memory, when memory space is needed.

See cache_replacement_policy for details

Default

memory_replacement_policy lru

### SQLite Page SizeCache Size 配置与优化 #### 1. Page Size 的配置与影响 SQLite 数据库文件由固定大小的数据页组成,这些数据页的大小可以通过 `PRAGMA page_size` 命令来设置。默认情况下,SQLite 使用 4KB 或者其他编译时指定的值作为页面大小[^1]。 调整页面大小会影响磁盘 I/O 性能和内存占用情况。较大的页面可以减少随机访问次数,从而提高性能;然而,过大的页面会增加单次读取的成本,并可能导致更多的内存浪费。 ```sql -- 查询当前数据库的页面大小 PRAGMA page_size; -- 设置新的页面大小(仅适用于尚未创建的新数据库) PRAGMA page_size = 8192; ``` 需要注意的是,在现有数据库上更改页面大小通常需要重新构建整个数据库文件[^4]。 --- #### 2. Cache Size 的配置与作用 Cache Size 定义了 SQLite 可以为每个连接分配的最大内存缓存数量,默认单位为页数。通过增大缓存大小,可以显著降低磁盘 I/O 开销,尤其是在频繁查询相同数据的情况下。但是,过多的缓存可能会消耗大量系统资源。 ```sql -- 查看当前缓存大小 PRAGMA cache_size; -- 将缓存大小设置为 20MB (假设每页 4KB,则对应约 5120 页面) PRAGMA cache_size = 5120; ``` 如果应用程序运行在多核环境中或者有多个并发连接,建议适当调大此参数以适应更高的负载需求[^3]。 --- #### 3. 综合考虑下的最佳实践 为了达到最优性能表现,应综合考量硬件条件、工作负载特点等因素: - **对于小型嵌入式设备**:由于受限于有限 RAM 资源,推荐保持较小的 page size 和适度的 cache size; - **针对服务器端应用**:允许更大的 page size 来匹配现代硬盘扇区尺寸 (通常是 4K),同时大幅扩展 cache size 提升吞吐率[^2]。 此外,启用 Write-Ahead Logging (WAL) 模式也能有效缓解传统回滚日志带来的瓶颈问题[^5]。 --- #### 示例代码展示如何动态修改这两个属性 下面是一个简单的 Python 程序演示如何利用 sqlite3 库去改变上述两个重要参数: ```python import sqlite3 conn = sqlite3.connect('example.db') cursor = conn.cursor() # 修改 page size 到 8 KB, 注意这一步只会在新建 DB 才生效 try: cursor.execute("PRAGMA page_size = 8192;") except Exception as e: print(f"Failed to set PRAGMA page_size due to {e}") # 动态调整 cache size 至 10 MB (~2621 pages at default 4k per page) cursor.execute("PRAGMA cache_size = 2621;") # 输出确认变更后的状态 print(cursor.execute("PRAGMA page_size;").fetchone()) print(cursor.execute("PRAGMA cache_size;").fetchone()) conn.close() ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值