V2EX mike163
 mike163 最近的时间轴更新
mike163

mike163

V2EX 第 20630 号会员,加入于 2012-05-11 15:41:05 +08:00
mike163 最近回复了
用 mlx 是不是比 ollama 性能更好?
345 天前
回复了 mike163 创建的主题 DNS 129.29.29.29 是个大坑货,千万别用
@winterbells 是的,119.29.29.29 ,写错了
一个具体的例子,用 ruby 写一个一个 自动生成 图片压缩的服务, 然后 转换成 go

require 'sinatra'
require 'open-uri'
require 'rmagick'
require 'time'


# Initialize cache
$image_cache = {}

# Cleanup task to remove stale cache entries
Thread.new do
loop do
sleep 8 * 60 * 60 # Sleep for 24 hours
current_time = Time.now
$image_cache.delete_if do |url, entry|
current_time - entry[:last_accessed] > 24 * 60 * 60
end
end
end

get '/' do
# Get the URL parameter
url = params['url']

# Ensure the URL is provided
halt 400, 'URL parameter is missing' if url.nil?

# Check cache for the image
if $image_cache.key?(url)
entry = $image_cache[url]
entry[:last_accessed] = Time.now
content_type 'image/jpeg'
return entry[:image_data]
end

# Fetch the image from the URL
begin
image_data = URI.open(url).read
rescue => e
halt 500, "Failed to fetch image: #{e.message}"
end

# Read the image data using RMagick
begin
image = Magick::Image.from_blob(image_data).first
rescue => e
halt 500, "Failed to read image: #{e.message}"
end

# Resize the image while maintaining the aspect ratio
begin
resized_image = image.change_geometry("360") do |cols, rows, img|
img.resize(cols, rows)
end
rescue => e
halt 500, "Failed to resize image: #{e.message}"
end

# Convert the resized image to a blob
resized_image_data = resized_image.to_blob

begin
# Convert the resized image to a blob
resized_image_data = resized_image.to_blob
rescue Magick::ImageMagickError => e
puts "Error: #{e.message}"
puts "Backtrace: #{e.backtrace.join("\n")}"
end

# Store the resized image in the cache
$image_cache[url] = {
image_data: resized_image_data,
last_accessed: Time.now
}

# Send the resized image data to the user
content_type 'image/jpeg'
resized_image_data
end%

转换后的代码,

package main

import (
"bytes"
"fmt"
"image"
"image/jpeg"
"log"
"net/http"
"sync"
"time"

"github.com/nfnt/resize"
)

const (
port = 8300
cacheCleanupInterval = 8 * time.Hour
cacheExpiratiOnTime= 24 * time.Hour
)

type CacheEntry struct {
ImageData []byte
LastAccessed time.Time
}

var (
imageCache = make(map[string]*CacheEntry)
cacheMutex sync.RWMutex
)

func main() {
go cleanupCache()

http.HandleFunc("/", handleRequest)
log.Printf("Server starting on port %d", port)
log.Fatal( http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}

func cleanupCache() {
for {
time.Sleep(cacheCleanupInterval)
currentTime := time.Now()
cacheMutex.Lock()
for url, entry := range imageCache {
if currentTime.Sub(entry.LastAccessed) > cacheExpirationTime {
delete(imageCache, url)
}
}
cacheMutex.Unlock()
}
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
if url == "" {
http.Error(w, "URL parameter is missing", http.StatusBadRequest)
return
}

cacheMutex.RLock()
if entry, ok := imageCache[url]; ok {
entry.LastAccessed = time.Now()
cacheMutex.RUnlock()
w.Header().Set("Content-Type", "image/jpeg")
w.Write(entry.ImageData)
return
}
cacheMutex.RUnlock()


log.Printf("Fetching image: %v\n", url)

resp, err := http.Get(url)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to fetch image: %v", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()

img, _, err := image.Decode(resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to decode image: %v", err), http.StatusInternalServerError)
return
}

resizedImg := resize.Resize(360, 0, img, resize.Lanczos3)

var buf bytes.Buffer
if err := jpeg.Encode(&buf, resizedImg, nil); err != nil {
http.Error(w, fmt.Sprintf("Failed to encode resized image: %v", err), http.StatusInternalServerError)
return
}

cacheMutex.Lock()
imageCache[url] = &CacheEntry{
ImageData: buf.Bytes(),
LastAccessed: time.Now(),
}
cacheMutex.Unlock()

w.Header().Set("Content-Type", "image/jpeg")
w.Write(buf.Bytes())
}%
@alexhx 现代大语言模型可以自动 用目标语言合适的模块 替换。这是真正厉害的地方。
@nekoharuya 以前模型能力不行。新代码质量很差。现在大模型编程能力上来了,写的代码质量比较好。
@aizya 没看懂,我是直接把代码扔给大模型,让塔用目标语言写一份新的
@njylll 1 部署,性能,并发支持等。2 实现同样功能,ruby 代码量可以是 go 的一半还少。
@Mark24 我知道,就是不想用 asyncore ruby ,让大语言模型帮我转成 go 只花了 1 分钟。如果用 async ,至少折腾半天。 每个语言都有自己的优势。
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2818 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 23ms UTC 13:37 PVG 21:37 LAX 06:37 JFK 09:37
Do have faith in what you're doing.
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86